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

Php 以编程方式更改活动Drupal 7主题,php,drupal,drupal-7,drupal-modules,drupal-theming,Php,Drupal,Drupal 7,Drupal Modules,Drupal Theming,以编程方式更改活动Drupal 7主题的正确方法是什么?我在Drupal 6中使用了$custom_theme,但在Drupal 7中不起作用。您可以使用: 如果您需要根据路径进行选择,那么最好的方法是覆盖特定菜单项的主题回调 虽然我不确定您想要更改主题的条件是什么,但是如果您想要基于url、节点类型、分类术语、查看页面等更改主题,那么您可以使用上下文模块来处理,该模块将为您执行此操作,您甚至不必编写一行代码。看看这个: 这是一个非常有用的模块,它与几乎所有著名的模块(如面板、omega主题、d

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

您可以使用:


如果您需要根据路径进行选择,那么最好的方法是覆盖特定菜单项的主题回调

虽然我不确定您想要更改主题的条件是什么,但是如果您想要基于url、节点类型、分类术语、查看页面等更改主题,那么您可以使用上下文模块来处理,该模块将为您执行此操作,您甚至不必编写一行代码。看看这个:


这是一个非常有用的模块,它与几乎所有著名的模块(如面板、omega主题、delta等)都有很好的集成。

Drupal变量
theme\u default
是您必须设置以使用函数切换主题的模块

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

/**
 * 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);
}
虽然
variable\u set()
适用于
hook\u install()
hook\u update\N()
,但不应在模块中使用它。调用
variable\u set()
会清空缓存引导表,这对繁忙站点的性能造成严重影响

如果你不需要语境的全部力量,我会推荐你。然而,上下文很容易导出以进行版本控制,而据我所知,没有办法导出mekey规则

/**
 * 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);
}