2

I'm trying to theme status messages, I want to change the HTML of the message.

Drupal default is

 <div class="message status">message!</div>

What I want to achieve is

 <div class="alert alert-success">message!</div>

I have used theme_status_messages to modify 'messages' but how can I modify the type (e.g. 'status'/'warning', etc)?

This is current 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=\"alert $type\">\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 .= $messages[0];
    }
    $output .= "</div>\n";
  }
  return $output;
}

I've changed this line

$output .= "<div class=\"messages $type\">\n";

to

$output .= "<div class=\"alert $type\">\n";

as you can see in my code above, but how can I change the options for $type?? I want my types to be alert-success, alert-error, alert-warning, rather than status, error, warning.

Anyone got an idea?

thanks!

8
  • I have no clue what you're asking for.
    – Beebee
    Mar 13, 2015 at 16:25
  • 1
    @caustic Why not just switch your CSS classes to correspond with the default Drupal ones? Mar 13, 2015 at 16:27
  • Can't just do: $output .= "<div class=\"messages alert-$type\">\n"; ?
    – Beebee
    Mar 13, 2015 at 16:33
  • @slawrence10 I'm using Bootstrap and requirements are that theres no additional classes for Bootstrap-served elements. Bootstrap has it alert alert-success format
    – caustic
    Mar 13, 2015 at 16:39
  • @Beebee I caaaaan, but that still doesn't work. Because $type isn't correct. alert-$type will give me alert-status rather than alert-success. Any ideas?
    – caustic
    Mar 13, 2015 at 16:40

1 Answer 1

1

This is just some good old PHP logic, is this what you're after?

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

  $status_heading = array(
    'status' => t('Status message'),
    'error' => t('Error message'),
    'warning' => t('Warning message'),
  );

  // Map our drupal statuses to bootstrap classes
  $bootstrap_statuses = array(
    'status' => 'alert-success',
    'error' => 'alert-error',
    'warning' => 'alert-warning',
  );

  foreach (drupal_get_messages($display) as $type => $messages) {
    // Grab the bootstrap class that corresponds to $type
    $bootstrap_class = $bootstrap_statuses[$type];
    $output .= "<div class=\"alert $bootstrap_class\">\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 .= $messages[0];
    }
    $output .= "</div>\n";
  }
  return $output;
}

You could even put the $bootstrap_classes and $status_heading arrays together. But I separated them for simplicity

2
  • Thank you very much , didn't realise it was so simple, thought that maybe there was another hook for that :) Works anyway!
    – caustic
    Mar 13, 2015 at 17:29
  • You're welcome. Sometimes it's worth to take a step back and think "Can I solve this with good ol' PHP?" :) I've been there so many times.
    – Beebee
    Mar 13, 2015 at 17:33

Your Answer

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

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