5

I'm trying to add a chapter tag (§) in front of a title for content type 'ABC' in a Drupal 7 site.

I tried the following:

function mytheme_preprocess_node(&$variables) {
  if ($variables['view_mode'] == 'full' && node_is_page($variables['node'])) {
    $variables['classes_array'][] = 'node-full';
  }
  //watchdog("mytheme_preprocess_node", 'items %items', array('%items' => print_r($variables['node'], true)));
  $node = $variables['node'];
  if ($node->type=='abc') {
    $node->title = 'this is a test' . $node->title; 
    $variables['node'] = $node;
    watchdog("mytheme", 'items %items', array('%items' => print_r($variables['node'], true)));
  }
}

I did see the message output in the log, however updated title is not showing up on the site.

3
  • Is your theme picking up on the preprocess function? If you've justed added it you may need to clear the cache. Commented Jan 12, 2012 at 17:16
  • Not sure if this is your problem but you don't need to do $variables['node'] = $node; the variable is already equal to the node. Commented Jan 12, 2012 at 17:19
  • 2
    I suggest you use the Devel module for debugging (and for other tasks too), and some of its functions like dsm(), etc., instead of logging everything using watchdog(). By the way, another really useful module is Theme developer, but it's rather useful for theming (not surprisingly). Anyway, +1 for your question.
    – Sk8erPeter
    Commented Sep 25, 2012 at 16:36

1 Answer 1

7

Every implementation of hook_preprocess_node() made from a module (or a theme) is executed after template_preprocess_node(), which contains the following code:

  $variables['node_url']  = url($uri['path'], $uri['options']);
  $variables['title']     = check_plain($node->title);
  $variables['page']      = $variables['view_mode'] == 'full' && node_is_page($node);

template_preprocess_node() is the preprocess function executed from the Node module, which is the module that defines theme_node().

The template file is then using $title for the page title.

  <?php print render($title_prefix); ?>
  <?php if (!$page): ?>
    <h2<?php print $title_attributes; ?>>
      <a href="<?php print $node_url; ?>"><?php print $title; ?></a>
    </h2>
  <?php endif; ?>
  <?php print render($title_suffix); ?>

If you want to change the value for title, you should change the value of $title, in your preprocess function.

The execution order of preprocess functions is explained in the documentation for theme(). In that documentation "HOOK" is a placeholder for the type of the preprocess function, e.g. "node," "html," and "page." Between the rendering engine used for the theme, a module, and the currently set theme, the preprocess function implemented from the theme is the last one to be called. This helps if there is another module that alters the title, and you want to use the one set from the theme.

What I reported is also valid for Drupal 6.

1
  • Thank you. Changing $variables['title'] in mytheme_preprocess_node solved my problem Commented Jan 12, 2012 at 18:39

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.