0

I have a module that implements HOOK_theme() to define a 'petition' theme like so:

function mymod_theme() {
  return [
    'petititon' => ['variables' => ['foo'=>NULL], 'template' => 'petition']
  ];
}

Which I then use like:

$some_render_array[] = [ '#theme' => 'petition', '#foo' => 'Bar' ];

Which is then rendered by a petition.tpl.php template file.

However, I would like to do this:

$some_render_array[] = [
  '#theme' => 'petition',
  '#theme_hook_suggestions' => ['petition__1'],
  '#foo' => 'Bar'
];

...and have it look for petition--1.tpl.php, in the same way you can for nodes, pages etc.

But this doesn't seem to work. Is it possible to have a theme that can take a template suggestion? I have a bunch of things to render which all require the same processing but use different templates.

1 Answer 1

0

To suggest specific templates you probably need to do it in your theme's template file, use the hook_preprocess_HOOK where the first hook is the name of your theme and seconde HOOK is either page to suggest a page template or node to suggest a node page.

Here's an example

/**
 * Implementation of hook_preprocess_HOOK().
 */
function mymodule_preprocess_page(&$variables){
  // change the condition to whatever you like
  if(isset($variables['node']) && arg(2) != 'edit')
    $variables['theme_hook_suggestions'][] = 'page__'. $variables['node']->type;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.