【问题标题】:how do i include a tpl file in my module drupal 7我如何在我的模块drupal 7中包含一个tpl文件
【发布时间】:2011-11-23 12:45:50
【问题描述】:

我正在使用 hook_preprocess_node() 构建一个模块 我使用 hook_entity_info_alter() 为名为“vacancy_teaser”的节点实体创建了一个新的视图模式

这显示在我的节点显示设置和视图中

所以我想在使用此视图模式时使用我的模块中包含的模板。

我的代码:

/**
* Implements hook_preprocess_node().
*/
function vacancies_preprocess_node(&$vars) {
  if($vars['view_mode'] == 'vacancy_teaser') {
    $vars['theme_hook_suggestions'][] = 'node_vacancy_teaser';
  }
} 

我的模板文件被称为:'node-vacancy-teaser.tpl.php',但未在我的视图输出中使用 $vars['view_mode'] == 'vacancy_teaser' 在视图中。 (已测试)

但是$vars['theme_hook_suggestions'][] = 'node_vacancy_teaser'; 在哪里寻找模板文件?不知何故,它没有被包含/使用。

显然,在 drupal 7 中,出于某种原因需要使用双下划线。 放置在活动模板文件夹中的 node_vacatures_vacancy_teaser.tpl.php 似乎可以解决问题……尽管我认为这不是一个巧妙的解决方案,因为 tpl.php 文件与模块分离.

【问题讨论】:

    标签: drupal-7


    【解决方案1】:

    一定要在 hook_theme 实现中指定模板文件。 examples project 非常适合了解如何执行此类操作的详细信息。具体来说,查看theming_example module中的theming_example_theme() function...

    function theming_example_theme() {
      return array(
        // …
        'theming_example_text_form'  => array(
          'render element' => 'form',
          // In this one the rendering will be done by a tpl.php file instead of
          // being rendered by a function, so we specify a template.
          'template' => 'theming-example-text-form',
        ),
      );
    }
    

    【讨论】:

    • 所以为了清楚起见,您是在建议我使用 hook_theme 而不是 hook_preprocess_node() 并添加一个 theme_hook_suggestion?
    • 是的,您在搜索了 3 天后发现了这个问题 how to load a temlate file form a module
    【解决方案2】:

    不要追加到$vars['theme_hook_suggestions']数组的末尾,试试:

    array_unshift($vars['theme_hook_suggestions'], 'node_vacancy_teaser');

    这会将你的建议传递到数组的最前面,它会被首先找到。很可能因为您将它附加到数组的末尾,Drupal 会首先找到现有的主题建议并使用它(例如 node.tpl.php)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多