4

I'm building a module using hook_preprocess_node() I've made a new view mode for the node entity called ´vacancy_teaser´ using the hook_entity_info_alter()

this shows up in my node display settings and view

so I want to use the template included in my module when this view mode is used.

my code:

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

my template file is called: ´node-vacancy-teaser.tpl.php´ but is not used in the output of my view $vars['view_mode'] == 'vacancy_teaser' in the view. ( tested )

but where does $vars['theme_hook_suggestions'][] = 'node_vacancy_teaser'; looks for the template file? somehow it's not included / used.

apparently in drupal 7 useing dubble underscores is required for some reason. node_vacatures_vacancy_teaser.tpl.php placed in the active template folder seems to do the trick... although I don't think this is a neat solution since the tpl.php file is separated from the module.

FLY
  • 2,379
  • 5
  • 29
  • 40

2 Answers2

7

Be sure to specify the template file in the hook_theme implementation. The examples project is great for finding out the details of how to do things like this. Specifically, check out the theming_example_theme() function in the 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',
    ),
  );
}
shasi kanth
  • 6,987
  • 24
  • 106
  • 158
Matt V.
  • 9,703
  • 10
  • 35
  • 56
  • so just to be clear you are suggesting that I use hook_theme instead of hook_preprocess_node() and add a theme_hook_suggestion? – FLY Nov 24 '11 at 08:56
  • yes you were found this question after 3 days of searching [how to load a temlate file form a module](http://stackoverflow.com/questions/5305114/drupal-7-how-to-load-a-template-file-from-a-module) – FLY Nov 25 '11 at 16:56
0

Instead of appending to the end of the $vars['theme_hook_suggestions'] array, try:

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

This will pass your suggestion to the front of the array, and it will be found first. Most likely since you are appending it to the end of the array, Drupal is finding an existing theme suggestion first and using it instead (such as node.tpl.php).

devslashnull
  • 56
  • 1
  • 4