hook_preprocess_page() does not seem to use the su

2019-09-09 09:27发布

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?

2条回答
狗以群分
2楼-- · 2019-09-09 09:59

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.

查看更多
甜甜的少女心
3楼-- · 2019-09-09 09:59

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.

查看更多
登录 后发表回答