4

I'm trying to implement pattern and template suggestion but It doesn't work.
Here is the implementation of hook_theme() with my pattern:

$theme['nm_home_page_zone'] = array(
  'pattern' => 'nm_home_page_zone__',
  'arguments' => array('nodes_content' => array(), 'nodes' => array()),
  'template' => 'zone-contenu',
),

And I call theme() with the following code:

$output .= theme(array('nm_home_page_zone__'.$rowZone->model.'_'.$rowZone->weight, 'nm_home_page_zone__'.$rowZone->model, 'nm_home_page_zone'), array(), array());

I got 2 template files (zone-contenu.tpl.php and zone-contenu--one.tpl.php) in my theme and my module (just to be sure).
The pattern and template suggestion should use zone-contenu--one.tpl.php in my case, but it doesn't work.

What I'm missing here?

apaderno
  • 28,547
  • 16
  • 75
  • 90
Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31

1 Answers1

5

I got the answer (which is pretty simple though) : the template file must be named as the theme hook (key)
So the changes are in the hook_theme :

$theme['zone-contenu'] = array(
  'pattern' => 'zone-contenu__',
  'arguments' => array('nodes_content' => array(), 'nodes' => array()),
  'template' => 'zone-contenu',
),


And that's it ! You're good to play with pattern :)

Keep in mind you can use dynamic template with theme() with a string like that :

theme('zone_contenu__fun__here', $hello);

In this example Drupal will automatically try to find zone-contenu--fun--here.tpl.php then zone-contenu--fun.tpl.php and finally zone-contenu.php

If you want to control the order of suggestion, give an array like above (FIFO) :

$output .= theme(array('nm_home_page_zone__'.$rowZone->model.'_'.$rowZone->weight, 'nm_home_page_zone__'.$rowZone->model, 'nm_home_page_zone'), array(), array());

Hope this help !

Plumillon Forge
  • 1,659
  • 1
  • 16
  • 31
  • According to http://drupal.stackexchange.com/questions/48302/use-of-pattern-in-hook-theme the pattern isn't actually used. Therefor you should name your template(prefix) the same as the theme hook. – Neograph734 Feb 23 '14 at 11:42