4

the drupal_get_messages() function is returning nothing when run from page--front.tpl in my custom theme. Do I have to add something to page.tpl.php to build this?

I have a print_r set up and it's always entirely empty even though in the admin panel I've got all types of messages

5

By the time your page template is built, drupal_get_messages() has already been called (by way of theme_status_message(), which is called from template_preprocess_page()).

Since drupal_get_messages() clears the message cache unless the second argument to the function is explicitly set to FALSE, and theme_status_message() doesn't provide that second argument, the next time it's called the $_SESSION['messages'] var has already been cleared.

You already have access to $messages in your page template file, but if you're looking to get hold of each individual message you'll probably have to do this in a preprocess function:

function MYTHEME_preprocess_page(&$vars){ 
  $messages = drupal_get_messages(NULL, TRUE);
  $vars['message_list'] = $messages;
}
4
  • So it's coming up as NULL for me despite no longer calling drupal_get_messages() is that normal? what should i expect to see in my page--front.tpl as far as messages? where would i put a preprocess function? – Damon May 23 '12 at 13:50
  • The preprocess function goes in either a module file or your theme's template.php file...what are you actually trying to do, break the messages down into individual strings? Because if you're just trying to output the standard message area in page.tpl.php you just need to write <?php echo $messages; ?> wherever you want it to appear – Clive May 23 '12 at 13:56
  • ya I've been writing <?php echo $messages; ?>` and it's been blank (though I see the messages in my admin panel). Overall, I'm trying to get messages from a webform in a lightbox to show up on the homepage after it reloads.. – Damon May 23 '12 at 15:37
  • The messages are stored in $_SESSION vars and cleared on each page load, that's probably why you're having the problem – Clive May 23 '12 at 15:39
1

if you have a message (error or successful) and it does not appear in your tpl may be you don't print messages in page--front.tpl

<?php print $messages; ?>

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.