以编程方式更改活动的 Drupal 7 主题
以编程方式更改活动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
hook_custom_theme ()
:如果您需要根据路径进行选择,那么最好的方法是覆盖特定菜单路由器项的
主题回调
。 请参阅此处的示例。You can use
hook_custom_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.虽然我不确定你想要更改主题的条件是什么,但如果你想根据 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.
Drupal 变量
theme_default
是您必须使用 variable_set 函数。您可以通过 hook_update_N 更改默认主题 如果您已经安装了自定义模块。另请确保调用 hook_install 中的代码 在安装期间运行它,以防您想与其他人共享您的模块并希望在安装期间更改活动主题。
Drupal variable
theme_default
is the one you have to set to switch theme using variable_set function.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.
虽然
variable_set()
适用于hook_install()
或hook_update_N()
,但您不应在模块中使用它。调用variable_set()
会清空cache_bootstrap表,这对繁忙的站点来说会造成严重的性能影响。如果您不需要 Context 的全部功能,我会推荐 ThemeKey 模块。但是,上下文可以轻松导出以进行版本控制,但据我所知,无法导出 ThemeKey 规则。
While
variable_set()
will work forhook_install()
orhook_update_N()
, you should not use it within a module. Callingvariable_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.