2011-11-23 86 views
4

我使用hook_preprocess_node() 我做了一個名爲'vacancy_teaser'使用hook_entity_info_alter()我如何包括第三方物流文件在我的模塊的Drupal 7

節點實體一個新的視圖模式構建模塊

這顯示在我的節點顯示設置和視圖

所以我想使用模板中包含的模板,當使用這種視圖模式。

我的代碼:

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

我的模板文件名爲:'node空位,teaser.tpl.php'但在我看來 視圖$vars['view_mode'] == 'vacancy_teaser'的輸出不使用。 (測試過)

$vars['theme_hook_suggestions'][] = 'node_vacancy_teaser';在哪裏尋找模板文件?不知何故,它不包括/使用。

顯然在drupal 7中使用dubble下劃線是由於某種原因需要的。 node_ vacatures _vacancy_teaser.tpl.php放置在活動模板文件夾似乎做的伎倆......雖然我不認爲這是一個整潔的解決方案,因爲tpl.php文件從模塊分開。

回答

7

務必在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', 
    ), 
); 
} 
+0

所以只是要清楚你是建議我使用hook_theme而不是hook_preprocess_node()並添加一個theme_hook_suggestion? – FLY

+0

是的你在3天的搜索後發現這個問題[如何加載模塊的temlate文件](http://stackoverflow.com/questions/5305114/drupal-7-how-to-load-a-template-文件從-A-模塊) – FLY

0

而是追加到$vars['theme_hook_suggestions']數組的末尾,請嘗試:

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

這將您的建議傳遞給陣列前端,它會首先被發現。最有可能的是,因爲您將它追加到數組的末尾,Drupal首先查找現有的主題建議,然後使用它(例如node.tpl.php)。

相關問題