Modify default rendering of a custom content type defined in a module I am developing a module for importing, editing and exporting DocBook(XML) documents. In this module I define two new content types: docbook and  *docbook_section*. I have also defined new fields like: author, keywords, etc.

The default rendering of these content types is not quite handsome, so I would like to change and improve it. The classical Drupal way is to copy the template node.tpl.php to node--docbook.tpl.php and customize it. But this modification is done on the current theme, after the module has been installed. I would like to do the modification inside the module, so that as soon as it is installed it has a decent default look, without having to make any external modifications. Themes should also be able to override this template, in order to customize the look.

It may seem reasonable to place node--docbook.tpl.php inside the module, but that way it will not work: the theme/rendering engine will not be able to find and use the template. To solve this problem, I have defined hook_theme() on the module like this:

/**
 * Implements hook_theme().
 */
function docbook_theme($existing, $type, $theme, $path) {
  return array (
    'node__docbook' =&gt; array (
      //'variables' =&gt; array('view' =&gt; NULL,),
      'template' =&gt; 'node--docbook' ,
      'base hook' =&gt; 'node',
      'path' =&gt; drupal_get_path('module', 'docbook'),
      'preprocess functions' =&gt; array('template_preprocess_node__docbook'),
    ),
  );
}


This time it indeed finds the template node--docbook.tpl.php inside the module. Themes can also override the template if they define node--docbook.tpl.php. But I have some problems.

First, I call nowhere the function theme('docbook', $variables), so I don't see where I can assign the variables. Another option can be to assign them on the preprocess function like this:

function template_preprocess_node__docbook(&amp;$variables) {
  $variables['test'] = 'ABC';
}


But this doesn't work either, and in fact, it seems that the function template_preprocess_node__docbook() is not called at all, although it has been listed on 'preprocess functions' as well (at the definition of hook_theme()).

So, I am stuck. Although I have been working with Drupal7 for two years, theme and template processing still seems to me like a huge maze and I am not able to find my way around. Can somebody help me?
This is the way that I solved this problem:

/**
 * Implements hook_theme().
 */
function docbook_theme($existing, $type, $theme, $path) {
  return array (
    'node__docbook' =&gt; array(
      'template' =&gt; 'node--docbook',
    ),
    'node__docbook_section' =&gt; array(
      'template' =&gt; 'node--docbook-section' ,
    ),
    'field__field_dbw_author' =&gt; array(
    ),
  );
}

/**
 * Implements hook_preprocess_HOOK().
 */
function docbook_preprocess_node(&amp;$variables) {
  //krumo($variables);  //debug

  if ($variables['type'] == 'docbook') {
    $variables['variable1'] = 'hello1';
  }

  if ($variables['type'] == 'docbook_section') {
    $variables['variable2'] = 'hello2';
  }
}

/**
 * Implementing theme function theme_HOOK().
 */
function theme_field__field_dbw_author($variables) {
  //krumo($variables);  //debug

  $output = '';

  . . . . . . . . . . . . . . .

  return $output;
}


See more details on these commits:


https://github.com/dashohoxha/DocBookWiki/commit/0e15266fdfa9000bd32d2ddabce036b21fa55930
https://github.com/dashohoxha/DocBookWiki/commit/0fee64d261f66878166c215559234e01e185e6f1
https://github.com/dashohoxha/DocBookWiki/commit/0fb4ac1ca7da38067c647f362c22d167c62f5ae6
https://github.com/dashohoxha/DocBookWiki/commit/db15d8af013bfc91143029c84e7cd0134400af85
https://github.com/dashohoxha/DocBookWiki/commit/c9250ba89094fc2c8fa83945ffbee0be25d42d84


I would expect these solutions to work as well: 

function docbook_preprocess(&amp;$variables, $hook) { 
  if ($hook == 'node__docbook') { 
    $variables['my_new_variable'] = 'hello'; 
  } 
} 

function docbook_preprocess_node__docbook(&amp;$variables) { 
  $variables['my_new_variable'] = 'hello'; 
} 

function template_preprocess_node__docbook(&amp;$variables) { 
  $variables['my_new_variable'] = 'hello'; 
} 


But somehow they don't work. Either my understanding and expectations are wrong, or there is something wrong on the implementation of Drupal.