How does hook_theme () work?

I find it difficult to understand what hook_theme () does.

I understand that this is due to the fact that you can override templates.

I watched:

$theme_hooks = array( 'poll_vote' => array( 'template' => 'poll-vote', 'render element' => 'form', ), 'poll_choices' => array( 'render element' => 'form', ), 'poll_results' => array( 'template' => 'poll-results', 'variables' => array('raw_title' => NULL, 'results' => NULL, 'votes' => NULL, 'raw_links' => NULL, 'block' => NULL, 'nid' => NULL, 'vote' => NULL), ), 'poll_bar' => array( 'template' => 'poll-bar', 'variables' => array('title' => NULL, 'votes' => NULL, 'total_votes' => NULL, 'vote' => NULL, 'block' => NULL), ), ); 

Could you give an example of how this works?

+8
drupal drupal-7 drupal-templates hook
source share
3 answers

It provides a place where a module can define its own topics, which can then be redefined by any other module / theme. It will also provide the ability for any module to use a hook such as mymodule_preprocess_theme_name to modify the variables passed to the theme's final function or template file.

There are two ways to initialize a theme function:

 theme('poll_results', array('raw_title' => 'title', 'results' => $results, etc...)); 

and

 $build = array( '#theme' => 'poll_results', '#raw_title' => 'title', '#results' => $results, etc... ); // Note the '#' at the beginning of the argument name, this tells Drupal `render` function that this is an argument, not a child element that needs to be rendered. $content = render($build); // Exact equivalent of calling the previous example now that you have a render array. 

Please keep in mind that you should avoid calling the topic directly () (for documentation in the .inc theme), as it:

  • Caching Circumvents.
  • The default restriction on types defined in hook_element_info (), including attached assets
  • Zeroes the stages of pre_render and post_render.
  • JavaScript circulators contain information.

In Drupal 8, topic () is a private function, _theme (). See www.drupal.org/node/2173655 for more details.

When you compare two of these elements with the poll_results element in the above example, you can probably decide what happens ... since PHP is not a strongly typed language. Drupal provides "named arguments" through an array passed to the theme function, or as hashed keys in a rendering array.

As for the "render element", this basically tells the system that this theme function will be called using a rendering array with one named argument (in this case, form ). The code will look something like this:

 $build = array( '#theme' => 'poll_choices', '#form' => $form ); 

This will cause the theme function to be used in the $form variable as the only argument.

Regarding the template key:

 'poll_vote' => array( 'template' => 'poll-vote', 'render element' => 'form', ) 

defines a topic called poll_vote that uses a template file (hence the template key) with the name "poll-vote.tpl.php" (this is by agreement). The path to this template file will be found using the path to the module that implements it (for example, modules / poll / poll-vote.tpl.php), so you can place the template files in subfolders in the main module folder.

There are two ways to return the result for a theme function, by implementing the name of the physical function (in this case it will be theme_poll_vote ) or by using a template file. If the template key is empty, Drupal will assume that you have implemented a physical function and try to call it.

Template files are preferable if you have a fair bit of HTML to output for the theme, or you just don't like writing HTML in lines inside PHP (I personally don't). However, in any case, the variables passed when the theme was called (either using theme() or the visualization array, as described above) are themselves transferred to the template file or theme function. So:

 function theme_poll_results(&$vars) { $raw_title = $vars['raw_title']; $results = $vars['results']; // etc... } 

If you used the template file instead of the same method, the variables will be available as $raw_title , $results , etc., since Drupal runs extract in $vars before $vars template file.

I’m sure there’s a lot that I missed, but if you have any more specific questions, try it and I will try to help.

+22
source share

Drupal 6

I’ve been stuck with this all day and have now successfully implemented, so share your location here, maybe this will help to understand hook_theme .

There are 3 steps:

  • hook_theme

     function YOURMODULENAME_theme() { return array( 'xxx_xxx' => array( 'template' => 'xxx-xxx', // define xxx-xxx.tpl.php inside module 'arguments' => array('xxx' => null), //define $xxx so it will available in your xxx-xxx.tpl.php ), ); } 
  • echo / return theme in your .tpl or any .module

     $output = theme('xxx_xxx', $xxx); 
  • Now the variable is magically available to you xxx-xxx.tpl.php .

     <?php echo $xxx ?> 

Note: you can pass $ xxx as an array, an object, or something else :)

+3
source share

There is another way: (can be found in the Bartik topic)

The scenario here is that we have created our own module and want to override the default output to say node only with the name "zzz".
We do not know and do not care about how the default output signal is generated. All we need to do is tell Drupal to use our own template file (node ​​- custom - name.tpl.php) to display this particular node.

These steps are:

  • Tell Drupal where our template file is located. (Keep in mind that this function takes effect only once after clearing the Drupal cache):

     // Implements hook_theme() function mymodulename_theme() { $theme = array(); $theme['node__custom__name'] = array( 'render element' => 'node', 'template' => 'path_from_mymodule_root/node__custom__name', ); return $theme; } 
  • Tell Drupal where and when to use it.

     //Implements hook_preprocess_node() function mymodulename_preprocess_node($vars) { if($vars['node']->title == 'zzzz') { $vars['theme_hook_suggestions'][] = 'node__custom__name'; ... your other code ... } } 

    Now Drupal will use our template file only for this particular case, provided that the node - custom - name.tpl.php 'file is in the declared path, otherwise it will continue to search in accordance with the names of the agreement proposals for the backup template.

0
source share

All Articles