Question

How can I wrap $messages in a div using template.php file?

This is my code:

function THEME_status_messages($variables) {
  $display = $variables['display'];
  $output = '';

  $status_heading = array(
    'status' => t('Status message'),
    'error' => t('Error message'),
    'warning' => t('Warning message'),
  );
  foreach (drupal_get_messages($display) as $type => $messages) {
    $output .= "<div class=\"message $type\"><a class=\"message-close\"></a>\n";
    if (!empty($status_heading[$type])) {
      $output .= '<h2 class="element-invisible">' . $status_heading[$type] . "</h2>\n";
    }
    if (count($messages) > 1) {
      $output .= "<ul>\n";
      foreach ($messages as $message) {
      $output .= '<li>' . $message . "</li>\n";
    }
    $output .= "</ul>\n";
    }
    else {
      $output .= reset($messages);
    }
    $output .= "</div>\n";
    }
    return $output;
}

I need to wrap so the result it's this:

<div id="messages">
  <div class="message warning">
    <a class="message-close"></a>
  </div>
  <div class="message error">
    <a class="message-close"></a>
  </div>
</div>

I'm not interested to in doing it in page.tpl.php like this because it will leave an empty div if there's no messages:

<div id="messages"><?php print render($page['messages']); ?></div>

So far I wasn't able to output the #messages div, every time I try something I break everything or nothing happens.

Thank you.

Was it helpful?

Solution

Change this:

return $output;

To this:

return '<div id="messages">' . $output . '</div>';
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top