我正在使用 hook_preprocess_node() 构建一个模块
我使用 hook_entity_info_alter() 为名为“vacancy_teaser”的节点实体创建了一个新的 View 模式
这显示在我的节点显示设置和 View 中
所以我想在使用此 View 模式时使用我的模块中包含的模板。
我的代码:
/**
* 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',但未在我的 View 输出中使用
$vars['view_mode'] == 'vacancy_teaser'
在 View 中。 (经测试)但是
$vars['theme_hook_suggestions'][] = 'node_vacancy_teaser';
在哪里?寻找模板文件?不知何故,它不包括/使用。显然在 drupal 7 中出于某种原因需要使用双下划线。
放置在事件模板文件夹中的 node_vacatures_vacancy_teaser.tpl.php 似乎可以解决问题……尽管我认为这不是一个巧妙的解决方案,因为 tpl.php 文件与模块分离。
最佳答案
一定要在 hook_theme 实现中指定模板文件。 examples project非常适合了解如何执行此类操作的详细信息。具体来说,查看theming_example_theme() function在 theming_example module …
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',
),
);
}
关于drupal-7 - 我如何在我的模块drupal 7中包含一个tpl文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8242415/