2011-11-16 312 views

回答

8

您可以使用hook_custom_theme()

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

如果需要立足选择的路径,然后去最好的办法是重写特定菜单路由器项目的theme callbackSee here for an example

1

尽管我不确定当您想要更改主题时需要什么条件,但是如果您想根据url,节点类型,分类术语,视图页等来更改主题,那么您可以使用Context模块,它会为你做这件事,你甚至不需要编写一行代码。检查了这一点:http://drupal.org/project/context

这是一个非常有用的模块,并与几乎所有著名的模块,如面板,欧米茄主题,三角洲不错整合等

0

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); 
} 
0

虽然variable_set()将为hook_install()hook_update_N()工作,你不应该在一个模块内使用它。调用variable_set()会清空cache_bootstrap表,这在繁忙网站上会严重影响性能。如果你不需要Context的全部功能,我会推荐ThemeKey module。但是,上下文很容易导出用于版本控制,而据我所知,无法导出ThemeKey规则。