0

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' => array (
      //'variables' => array('view' => NULL,),
      'template' => 'node--docbook' ,
      'base hook' => 'node',
      'path' => drupal_get_path('module', 'docbook'),
      'preprocess functions' => 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(&$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?

0

This is the way that I solved this problem:

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

/**
 * Implements hook_preprocess_HOOK().
 */
function docbook_preprocess_node(&$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:

I would expect these solutions to work as well:

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

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

function template_preprocess_node__docbook(&$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.

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.