1

I want to use template, which name is determined in runtime (specialized template). If such template is not defined, use fallback template with fixed name (like node-sometype.tpl.php is used before node.tpl.php):

return theme(
    array('my_theme_'.$node->type, 'my_theme'),
    $some_arguments 
);

If I do not declare specialized templates in my hook_theme, and just make my_theme_page.tpl.php files in theme directory, they are not used.

If I declare them (here themes are specialized by node type, so it is possible), Drupal always uses them, producing include/call_user_func error, instead of using fallback one.

$themes = array(
    'my_theme' => array(
        'arguments' => array('some_arguments'=>NULL),
        'template' => 'my_theme',
        'path' => $path,
    ),
);
foreach (node_get_types() as $type => $_)
    $themes['my_theme_'.$type] = array(
        'arguments' => array('some_arguments'=>NULL),
        'template' => 'my_theme_'.$type,
        'path' => $path,
    );

So the question is:

How to declare theme in module, without providing implementation, so it can be used with theme() fallback mechanism.

4

When you want to allow users to have more templates to choose from that are more specific, fx one for all nodes vs one for each node type, you should use what Drupal call template suggestions. This will allow you to have a single theme function, and not have to clear cache to add the extra options.

You can read more about template suggestions.

What you want to is to create a single theme function with hook theme:

 function module_theme() {
   return array(
     'module_theme_function' => array(
       ..
     ),
   );
 }

Then create a preprocess function for that theme function:

function template_preprocess_module_theme_function(&$vars) {
  // Add template suggestions:
  $vars['template_files'][] = 'my-template-file-' . $some_var;
}

This would allow you to use the my-template-file-X.tpl.php file where X equals the variable you added. You can use this technique to add as many template suggestions you like. The last ones added will take precedence if several of the options is defined.

| improve this answer | |
1

For actual templates, you simply want to define so called template suggestions, then you don't need to define them at all in hook_theme(). See Working with template suggestions.

For theme functions, it is also possible to set the pattern property in hook_theme(), see privatemsg_theme() for an example.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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