24

In Drupal 7, by default the $messages variable is printed in page.tpl.php.

Is there a way to print $messages in a node.tpl.php file (e.g. $node-mycontenttype.tpl.php).

Just putting <?php print $messages; ?> or <?php print render($page['messages']); ?> does not work.

There's a tutorial here for printing $messages in a block, but I would like to put it in my node.tpl.php file.

31

Actually, what you want to do is this:

function YOURTHEME_preprocess_page(&$variables) {
  // This disables message-printing on ALL page displays
  $variables['show_messages'] = FALSE;
}

function YOURTHEME_preprocess_node(&$variables) {
  $variables['messages'] = theme('status_messages');
}

How this works: Drupal core checks the value of show_messages in theme_process_page. If it is TRUE (default) then it calls theme('status_messages') as above, and effectively empties the messages storage, so calling the same theme function later will just return an empty string. However, if we set that variable to FALSE, this prevents said theme function from being called in process_page, allowing us to get all the messages using the same theme function, in a template of our choosing.

  • 1
    And if you only want this to take action on node pages, just wrap the $variables['show_message'] = FALSE with a conditional to see if(isset($variables['node'])). – areynolds Jan 18 '13 at 0:55
  • And you have to do the same for all other pages (views, panels, etc) – Maria Ioannidou Jul 21 '16 at 14:46
8

The answer in How to display messages from a block separately from $messages in page.tpl.php which you referenced in your question is a fine way to go. Essentially you need something like this where YOURTHEME is the name of your theme and this function should be in your template.php. I haven't actually run this code but have a crack at it.

function YOURTHEME_preprocess_node(&$vars) {
  $output = '';
  foreach (drupal_get_messages(NULL, FALSE) as $type => $messages) {
    $output .= "<div class=\"messages $type\">\n";
    if (count($messages) > 1) {
      $output .= " <ul>\n";
      foreach ($messages as $message) {
        $output .= '  <li>' . $message . "</li>\n";
      }
      $output .= " </ul>\n";
    }
    else {
      $output .= $messages[0];
    }
    $output .= "</div>\n";
  }

  $vars['messages'] = $output;
}

You should then be able to output $messages in your node.tpl.php

  • Thanks for the solution. I've tried it but it doesn't seem to work. I am using the following to print the $messages: <?php print $messages; ?> Is that correct? – big_smile Nov 10 '11 at 19:21
  • 8
    Be careful when taking stuff out of page.tpl.php. The node template is not used for every page, so you might be missing messages that are displayed on non-node pages (like admin pages, for instance). When you're going to do this, I recommend that you keep a fallback in page.tpl.php that will show your messages when they're not intercepted by node.tpl.php yet. – marcvangend Nov 14 '11 at 16:18

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.