【问题标题】:How do I set the theme during cron run in my module code for Drupal 7如何在 Drupal 7 的模块代码中的 cron 运行期间设置主题
【发布时间】:2013-01-10 17:45:56
【问题描述】:

在 cron 运行期间,我有一个模块可以缓存许多节点的标记。我的问题是,在这个 cron 运行期间,来自渲染函数的任何标记都不会通过我的主题挂钩或模板。

如何从我的模块代码中选择主题?有钩子吗?有没有可以指定的功能?

最终,我希望能够做到这一点并获得与在 page_build 挂钩上运行它一样的结果:

render(node_view($node, 'teaser'));
render(node_view($node, 'mini_teaser'));

【问题讨论】:

标签: drupal drupal-7 drupal-theming drupal-hooks drupal-render


【解决方案1】:

Drupal 7 有一个钩子,允许模块更改当前启用的主题:hook_custom_theme()

请注意,用于调用该挂钩的代码如下。 (见menu_get_custom_theme()。)

// First allow modules to dynamically set a custom theme for the current
// page. Since we can only have one, the last module to return a valid
// theme takes precedence.
$custom_themes = array_filter(module_invoke_all('custom_theme'), 'drupal_theme_access');
if (!empty($custom_themes)) {
  $custom_theme = array_pop($custom_themes);
}
// If there is a theme callback function for the current page, execute it.
// If this returns a valid theme, it will override any theme that was set
// by a hook_custom_theme() implementation above.
$router_item = menu_get_item();
if (!empty($router_item['access']) && !empty($router_item['theme_callback']) && function_exists($router_item['theme_callback'])) {
  $theme_name = call_user_func_array($router_item['theme_callback'], $router_item['theme_arguments']);
  if (drupal_theme_access($theme_name)) {
    $custom_theme = $theme_name;
  }
}

由于 System 模块实现了该钩子,如果您在首先执行该钩子的模块中实现 hook_custom_theme()(例如,模块的简称为 custom_module),那么 System 模块可以覆盖您设置的主题模块。

一般情况下,设置全局$custom_theme 应该会得到相同的效果。确保设置的主题已启用。

【讨论】:

    猜你喜欢
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多