以编程方式更改活动的 Drupal 7 主题

发布于 2024-12-16 02:10:56 字数 100 浏览 2 评论 0原文

以编程方式更改活动 Drupal 7 主题的正确方法是什么?我在 Drupal 6 中使用了 $custom_theme 但它在 Drupal 7 中不起作用。

What is the correct way to change the active Drupal 7 theme programmatically? I used $custom_theme in Drupal 6 but it is not working in Drupal 7.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清君侧 2024-12-23 02:10:56

您可以使用 hook_custom_theme ()

function mymodule_custom_theme() {
  if ($some_condition_is_true) {
    return 'my_theme';
  }
}

如果您需要根据路径进行选择,那么最好的方法是覆盖特定菜单路由器项的主题回调请参阅此处的示例

You can use hook_custom_theme():

function mymodule_custom_theme() {
  if ($some_condition_is_true) {
    return 'my_theme';
  }
}

If you need to base your selection on the path then the best way to go is to override the theme callback for particular menu router items. See here for an example.

面犯桃花 2024-12-23 02:10:56

虽然我不确定你想要更改主题的条件是什么,但如果你想根据 url、节点类型、分类术语、视图页面等更改主题,那么你可以使用 Context 模块来处理它这是为您准备的,您甚至不必编写一行代码。看看这个: http://drupal.org/project/context

这是一个非常有用的模块,具有与几乎所有著名的模块(如面板、omega 主题、Delta 等)完美集成。

Although I am not sure what is the condition when you want to change the theme, but if you want to change the theme based on a url, node type, taxonomy term, view page etc then you can handle that using Context module which will do this for you and you don't even have to write a single line of code. Check this out: http://drupal.org/project/context

This is a very useful module and has nice integration with almost all famous modules like panels, omega theme, delta etc.

予囚 2024-12-23 02:10:56

Drupal 变量 theme_default 是您必须使用 variable_set 函数。

variable_set('theme_default', 'your_theme_name');

您可以通过 hook_update_N 更改默认主题 如果您已经安装了自定义模块。另请确保调用 hook_install 中的代码 在安装期间运行它,以防您想与其他人共享您的模块并希望在安装期间更改活动主题。

/**
 * Implements hook_update_N().
 */
function mymodule_update_7000() {
  $theme_list = array(
    'bootstrap',
    'mytheme',
    'shiny',
  );
  theme_enable($theme_list);
  $theme_default = 'mytheme';
  // The below code would change the default active theme to 'mytheme'
  variable_set('theme_default', $theme_default);
  $admin_theme = 'shiny';
  variable_set('admin_theme', $admin_theme);
}

Drupal variable theme_default is the one you have to set to switch theme using variable_set function.

variable_set('theme_default', 'your_theme_name');

You can change the default theme through a hook_update_N if you have a custom module already installed. Also make sure you call the code in hook_install to run it during install time in case you want to share your module with somebody else and want to change active theme during install time.

/**
 * Implements hook_update_N().
 */
function mymodule_update_7000() {
  $theme_list = array(
    'bootstrap',
    'mytheme',
    'shiny',
  );
  theme_enable($theme_list);
  $theme_default = 'mytheme';
  // The below code would change the default active theme to 'mytheme'
  variable_set('theme_default', $theme_default);
  $admin_theme = 'shiny';
  variable_set('admin_theme', $admin_theme);
}
不再见 2024-12-23 02:10:56

虽然 variable_set() 适用于 hook_install()hook_update_N(),但您不应在模块中使用它。调用variable_set()会清空cache_bootstrap表,这对繁忙的站点来说会造成严重的性能影响。

如果您不需要 Context 的全部功能,我会推荐 ThemeKey 模块。但是,上下文可以轻松导出以进行版本控制,但据我所知,无法导出 ThemeKey 规则。

While variable_set() will work for hook_install() or hook_update_N(), you should not use it within a module. Calling variable_set() empties the cache_bootstrap table, which is a serious performance hit on busy sites.

I would recommend the ThemeKey module if you don't need the full power of Context. However, contexts are easily exportable for versioning, while as far as I know there is no way to export ThemeKey rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文