2

Basically it was How to tell Drupal to look for templates in module directory? but I'll reword it, as I don't want to define a new theme (reason was already given).

What I want to do: in my module I have a file node--attach.tpl.php which is a modifed version of node.tpl.php. Now I want that, if view_mode = 'attach' then this file is used. Also, theme should be allowed to override it with its own node--attach.tpl.php in theme folder.

1 Answer 1

2

This code works for me:

function MYMODULE_theme_registry_alter(&$theme_registry) {
  $mod_path = drupal_get_path('module', 'MYMODULE');

  $theme_registry_copy = $theme_registry;       // munge on a copy
  _theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'pow', $mod_path);
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);

  $hooks = array('node');
  foreach ($hooks as $h) {
    if (!isset($theme_registry[$h]['theme paths'])) { 
      $theme_registry[$h]['theme paths'] = array(); 
    } 
    _MYMODULE_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);
  }
}

function _MYMODULE_insert_after_first_element(&$a, $element) {
  if(is_array($a)) { 
    $first_element = array_shift($a); 
    if ($first_element) { 
      array_unshift($a, $first_element, $element); 
    } 
    else { 
      array_unshift($a, $element); 
    }
  }
}

function MYMODULE_preprocess_node(&$vars) {
  if ($vars['view_mode'] == 'attach') {
    $vars['theme_hook_suggestions'][] = 'node__attach';
  }
}

I know you've seen nearly that exact code before from looking at your other question, but there are a couple of changes from the code in the article. It should work after clearing the caches (just once!)

Using the above I added a file called node--attach.tpl.php to my module folder, cleared the caches again, and the output from that template file was shown for the node.

Then I added node--attach.tpl.php to my theme folder, cleared the caches again, and the output from the new template file was shown.

3
  • OMG this WORKS! Thanks so much. And when I try to simplify it to <code>$theme_registry['node']['theme paths'] = array($mod_path);</code> (as it was not defined), it still works, of course. However, what does "pow" in the 5th line do?
    – jcisio
    Mar 29, 2012 at 5:41
  • I have no idea! I've tried to follow the functions through (it goes through quite a few, including phptemplate_theme() which is where 'pow' is eventually passed to) but I can't see why using a non-existent theme name would work. Perhaps that's the trick though...maybe passing a non-existent theme name somehow allows this override to happen. I'm interested though so I'll keep digging and let you know if I find anything
    – Clive
    Mar 29, 2012 at 7:44
  • I'm trying to use this in the form of a theme_hook_suggestion: node__news_item__template_a and using a node--news-item--template-a.tpl.php, this does not seem to work. When changing everything to only node--news-item, then it does work...
    – Ambidex
    Jul 25, 2013 at 8:24

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.