How to include tpl file in my drupal 7 module

I create a module using hook_preprocess_node () I created a new view mode for the node object called 'vacancy_teaser' using hook_entity_info_alter ()

this is displayed in the node display settings and displayed

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

my code is:

/** * 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 not used in the view of my view $vars['view_mode'] == 'vacancy_teaser' in the view. (verified)

but where is $vars['theme_hook_suggestions'][] = 'node_vacancy_teaser'; looking for a template file? somehow it did not turn on / was not used.

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

+4
source share
2 answers

Be sure to include the template file in the hook_theme implementation. The project is great for figuring out the details of how to do this. In particular, check the theming_example_theme () function in 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', ), ); } 
+7
source

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

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

This will bring your sentence to the front of the array, and it will be found first. Most likely, since you add it to the end of the array, Drupal first finds an existing topic sentence and uses it instead (for example, node.tpl.php).

0
source

Source: https://habr.com/ru/post/1382689/


All Articles