3

I'm trying to set a warning message based on a PHP if statement in my page.tpl.php file. I can get a message out using $messages but I cannot using drupal_set_message(). Should I be using the latter before page.tpl.php?

The problem is the $messages variable comes out as plain text, I'd like to use Drupal's message system if possible:

drupal_set_message(t('Don\'t panic!'), 'warning');
$messages = t('Don\'t panic!');
8

If you want to use Drupal's message system, and you want to do this at the theme-level, as opposed to writing a module, you would enter the code

drupal_set_message(t('Don\'t panic!'), 'warning');

into your theme's template.php file as a preprocess function. For instance, if you wanted to display this message on all content of the type 'page'

function YOURTHEME_preprocess_node(&$variables) {
 if ($variables['node']->type = 'page') {
  drupal_set_message(t('Don\'t panic!'), 'warning');
 } 
} 

It should then automatically show up in the $messages area whenever someone visits a 'page' content type.

Note: You might also need to flush your theme cache after making these changes.

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.