hook_preprocess_page() does not seem to use the suggested template file

霸气de小男生 提交于 2020-01-07 04:03:09

问题


I am suggesting a template file in the hook_preprocess_page() implementation done from a module, but the suggested template file doesn't seem to be used.

The template file is page--terminal-template.tpl.php, which is in the directory containing the module, and this is the implementation of hook_preprocess_page().

function terminal_preprocess_page(&$variables) {
  if (arg(0) == "terminal") {
    $variables['theme_hook_suggestions'][] = "page__terminal_template";
  }
}

Could anyone please help me?


回答1:


Preprocess and process functions can be implemented by modules. In fact, the documentation for theme() lists them when it shows in which order those functions are called.

The fact is that Drupal looks for the suggested template files in the theme directory. You have these alternatives:

  1. Put the template files your module is suggesting in the directory containing the theme currently used
  2. Follow what reported in Load view template on module activation to load the template files from the module directory
  3. Suggest the template files you want to use in a preprocess function implemented by a theme

Following what reported in the other question, you would be able to use the template file found in the module directory. The only problem is that you would be using a generic template that could be different from the default page template used from the currently enabled theme.

If you are adding template files for the currently enabled theme, you should call drupal_theme_rebuild() to make Drupal rescan the directory containing the template files, after you added the new template file to the theme.




回答2:


Actually, this hook can also be called from theme's template.php file along with module's hook.

Please refer Drupal 7 documentation here.

Say if your active theme is MY_THEME, then the code should be:

function MY_THEME_preprocess_page(&$variables) {
  if (arg(0) == "terminal") {
      $variables['theme_hook_suggestions'][] = "page__terminal_template";

  }
}

And the template suggestions will work.

Edit: This functionality can also be implemented with Modules using hooks.



来源:https://stackoverflow.com/questions/16625218/hook-preprocess-page-does-not-seem-to-use-the-suggested-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!