Running Drupal 6.20, am preparing a custom module set for publishing to community. These modules (MYMODULE) programmatically create CCK types (MYNODE)that we have been using custom templates to theme [node-mynode.tpl.php in the standard theme directory].

What I'd like to do is include with the module a default template [node-mynode.tpl.php] that is included in the mymodule/themes directory that would be used in the absence of a custom template in the theme directory. So, the priority would be:

  1. Default node-mynode.tpl.php provided in mymodule/themes
  2. Any other node-mynode.tpl.php that they would want to use to override my default by placing in the standard theme folder

It seems that what I'm looking for was described and commented on http://11heavens.com/theming-Drupal-6-from-the-module-layer and http://drupal.org/node/296871 , but I can't get those processes to work.

My code, which assumes the existence of a CCK-based node type called MYNODE and an already existing [theme based] node-mynode.tpl.php.


function mymodule_preprocess_node(&$vars) {    
//    drupal_set_message('node preprocess);
      $vars['template_file'][] = 'node-mynode';
}

function mymodule_theme_registry_alter(&$theme_registry) {
 //   print_r($theme_registry);
    $template = "node-mymodule";
    if (is_array($theme_registry['template_files'][$template])) {
        drupal_set_message($theme_registry['template_files']);
        $originalpath = array_shift($theme_registry[$template]['theme paths']);
        $modulepath = drupal_get_path('module', 'mymodule') . '/templates';

        array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
    }
}

Now, I've read that after 6.7, the theme_registry_alter system is no longer necessary, but I can't get this behaviour regardless of it's presence or absence. (http://drupal.org/node/223430). Potentially this can be accomplished through mymodule_theme as http://drupal.org/node/342350 suggests?

What would then be the correct interpretation? Something like: ?

function MYMODULE_theme(){
    return array(
        'node_MYNODE'=> array(
            'template'=>'node-MYNODE',            
        )
    );
}

function MYMODULE_menucallback(){
    return theme('node_MYNODE');
}

Comments

wrightnow’s picture

I seem to have achieved the functionality I want through the following code, utilizing hook_theme to define a path for node templates. The behaviour achieved is to look in my template directory for the initial template, and override by placing directly in the theme directory.

Kosher solution?

function MYMODULE_theme($existing, $type, $theme, $path) {
    return array(
        'node' => array(
            'arguments' => array('node' => NULL),
            'path' => drupal_get_path('module', 'MYMODULE') . '/templates',
        ),
    );
}
sign’s picture

not the best solution to be honest,
this will overwrite the default path for node.tpl.php, so if the theme does not have node.tpl.php it will trigger an array

function mymodule_theme_registry_alter(&$theme_registry) {
  // Get the path to this module
  $modulepath = drupal_get_path('module', 'mymodule');
  // Add the module path on top in the array of paths
  array_unshift($theme_registry['node']['theme paths'], $modulepath .'/templates');
}

// add a template file suggestion
function mymodule_preprocess_node(&$vars) {    
      $vars['template_files'][] = 'node-mycontent-type';
}

This will consider looking into mymodule/templates for node-mycontent-type.tpl.php

wrightnow’s picture

Very much appreciated Marek.

Thats the tweak on my original attempt that I'd been looking for - how on earth did you know to append "type" in the code below? I couldn't find that illumination in any of the documentation I stared at.

// add a template file suggestion
function mymodule_preprocess_node(&$vars) {    
      $vars['template_files'][] = 'node-mycontent-type';
}
njcheng’s picture

I know this is an old thread, but wanted to add this for anyone who stumbles across this thread looking to do this in D7. The solution below is from http://www.metachunk.com/blog/adding-module-path-drupal-7-theme-registry.

/**
 * Implements hook_theme_registry_alter()
**/
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) {
    _mymodule_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);
  }
}

/**
 * Helper function for re-ordering arrays (needed by theme_registry_alter)
*/
function _mymodule_insert_after_first_element(&$a, $element) {
  if(is_array($a)) {
    $first_element = array_shift($a);
    array_unshift($a, $first_element, $element);
  }
}
SKAUGHT’s picture

+1

mkadin’s picture

This issue is also very helpful in these matters: http://drupal.org/node/303586