4

I have a webform which works as a newsletter subscription form: it displays a textfield for an email and a button. When the button is clicked, a notification message is displayed on top of the page where any other messages are usually displayed.

However because the webform is a block and it's displayed in the sidebar, it would be more usable if the message is displayed right above the block, where visitor's focus currently is, instead of top-middle of the page. Is this something possible to implement?

To generalise: is it possible to display messages from different blocks/modules in different sections of the page instead of the unified $messages variable?

1 Answer 1

3

If the notification is being set in the $messages variable used in page.tpl.php you can emulate theme_status_messages() by adding this to your theme's template.php file

function YOURTHEME_preprocess_block(&$vars) {

  $block = $vars['block'];

  if ($block->module == 'MODULENAME' && $block->delta == 'DELTA') {
  $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;
  }
}

Then in the template file for just your block (e.g. block-MODULENAME-DELTA.tpl.php), print the $messages variable.

1
  • Thanks, this works great :-p Quick comment… if your theme is displaying the messages earlier in the layout, the theme may have already cleared the message queue (see drupal_get_messages). I was using Drupal 7 and the Omega theme, and I had to disable the theme message display before I could display the messages myself.
    – thirdender
    Aug 18, 2012 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.