Drupal 7 - System Messages

  • user notice: The custom_breadcrumbs_nodeapi() function called token replacement with an array rather than a string for $text in /home/redleafmedia/redleafmedia.com/sites/all/modules/token/token.module on line 263.
  • user notice: The custom_breadcrumbs_nodeapi() function called token replacement with an array rather than a string for $text in /home/redleafmedia/redleafmedia.com/sites/all/modules/token/token.module on line 263.

If you've worked with Drupal long, then you're familiar with its "message" system, where it will occasionally place small messages for users at the top of pages to let them know when something relevant has happened. For example, when a node is successfully saved Drupal will let the author node that everything went well. Or when there is an error in submitting a form, Drupal will place a message at the top of the page to let the author know what needs to be addressed in the form. This system is actually quite simple for developers to use at the API level. There are only two functions. The first is used to set a system message that you want users to see, and it is named drupal_set_message():

<?php
function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
  if (
$message) {
    if (!isset(
$_SESSION['messages'][$type])) {
     
$_SESSION['messages'][$type] = array();
    }

    if (
$repeat || !in_array($message, $_SESSION['messages'][$type])) {
     
$_SESSION['messages'][$type][] = $message;
    }

   
// Mark this page as being uncacheable.
   
drupal_page_is_cacheable(FALSE);
  }

 
// Messages not set when DB connection fails.
 
return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
}
?>

The function takes three arguments: the actual text of the message, the "type" of the message, and whether the message should be repeated if the same message has already been logged. The messages are saved in the $_SESSION array that PHP uses to store session information. For example, if you want to set a message of type "warning" with the text "example warning text", you would call drupal_set_message('example warning text', 'warning'). Drupal would set the value of $_SESSION['messages']['warning'] to 'example warning text'. Lastly it returns the messages portion of the $_SESSION array. That's it.

If you have some need to get the messages that have been set, you can use drupal_get_messages():

<?php
function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
  if (
$messages = drupal_set_message()) {
    if (
$type) {
      if (
$clear_queue) {
        unset(
$_SESSION['messages'][$type]);
      }
      if (isset(
$messages[$type])) {
        return array(
$type => $messages[$type]);
      }
    }
    else {
      if (
$clear_queue) {
        unset(
$_SESSION['messages']);
      }
      return
$messages;
    }
  }
  return array();
}
?>

It takes two arguments: the type of messages you want to read back and whether or not you want to clear the messages array after reading them. If you do not set a type, Drupal will return messages of all types. It should be noted that this returns an array of messages. So you would have to format the data into a string before placing it into a page's content.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.