Sets a message to display to the user.

Public

*
* Sets a message to display to the user.
*
* Messages are stored in a session variable and displayed in page.tpl.php via
* the $messages theme variable.
*
* Example usage:
* @code
* drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
* @endcode
*
* @param string $message
* (optional) The translated message to be displayed to the user. For
* consistency with other messages, it should begin with a capital letter and
* end with a period.
* @param string $type
* (optional) The message's type. Defaults to 'status'. These values are
* supported:
* - 'status'
* - 'warning'
* - 'error'
* @param bool $repeat
* (optional) If this is FALSE and the message is already set, then the
* message won't be repeated. Defaults to TRUE.
*
* @return array|null
* A multidimensional array with keys corresponding to the set message types.
* The indexed array values of each contain the set messages for that type.
* Or, if there are no messages set, the function returns NULL.
*
* @see drupal_get_messages()
* @see theme_status_messages()

</> CopyGet raw version
php
  1. /**
  2.  * Sets a message to display to the user.
  3.  *
  4.  * Messages are stored in a session variable and displayed in page.tpl.php via
  5.  * the $messages theme variable.
  6.  *
  7.  * Example usage:
  8.  * @code
  9.  * drupal_set_message(t('An error occurred and processing did not complete.'), 'error');
  10.  * @endcode
  11.  *
  12.  * @param string $message
  13.  * (optional) The translated message to be displayed to the user. For
  14.  * consistency with other messages, it should begin with a capital letter and
  15.  * end with a period.
  16.  * @param string $type
  17.  * (optional) The message's type. Defaults to 'status'. These values are
  18.  * supported:
  19.  * - 'status'
  20.  * - 'warning'
  21.  * - 'error'
  22.  * @param bool $repeat
  23.  * (optional) If this is FALSE and the message is already set, then the
  24.  * message won't be repeated. Defaults to TRUE.
  25.  *
  26.  * @return array|null
  27.  * A multidimensional array with keys corresponding to the set message types.
  28.  * The indexed array values of each contain the set messages for that type.
  29.  * Or, if there are no messages set, the function returns NULL.
  30.  *
  31.  * @see drupal_get_messages()
  32.  * @see theme_status_messages()
  33.  */
  34. function drupal_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
  35. if ($message) {
  36. if (!isset($_SESSION['messages'][$type])) {
  37. $_SESSION['messages'][$type] = array();
  38. }
  39.  
  40. if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
  41. $_SESSION['messages'][$type][] = $message;
  42. }
  43.  
  44. // Mark this page as being uncacheable.
  45. drupal_page_is_cacheable(FALSE);
  46. }
  47.  
  48. // Messages not set when DB connection fails.
  49. return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL;
  50. }

/**
* Returns all messages that have been set with drupal_set_message().
*
* @param string $type
* (optional) Limit the messages returned by type. Defaults to NULL, meaning
* all types. These values are supported:
* - NULL
* - 'status'
* - 'warning'
* - 'error'
* @param bool $clear_queue
* (optional) If this is TRUE, the queue will be cleared of messages of the
* type specified in the $type parameter. Otherwise the queue will be left
* intact. Defaults to TRUE.
*
* @return array
* A multidimensional array with keys corresponding to the set message types.
* The indexed array values of each contain the set messages for that type.
* The messages returned are limited to the type specified in the $type
* parameter. If there are no messages of the specified type, an empty array
* is returned.
*
* @see drupal_set_message()
* @see theme_status_messages()
*/

</> CopyGet raw version
php
  1. /**
  2.  * Returns all messages that have been set with drupal_set_message().
  3.  *
  4.  * @param string $type
  5.  * (optional) Limit the messages returned by type. Defaults to NULL, meaning
  6.  * all types. These values are supported:
  7.  * - NULL
  8.  * - 'status'
  9.  * - 'warning'
  10.  * - 'error'
  11.  * @param bool $clear_queue
  12.  * (optional) If this is TRUE, the queue will be cleared of messages of the
  13.  * type specified in the $type parameter. Otherwise the queue will be left
  14.  * intact. Defaults to TRUE.
  15.  *
  16.  * @return array
  17.  * A multidimensional array with keys corresponding to the set message types.
  18.  * The indexed array values of each contain the set messages for that type.
  19.  * The messages returned are limited to the type specified in the $type
  20.  * parameter. If there are no messages of the specified type, an empty array
  21.  * is returned.
  22.  *
  23.  * @see drupal_set_message()
  24.  * @see theme_status_messages()
  25.  */
  26.  
  27. function drupal_get_messages($type = NULL, $clear_queue = TRUE) {
  28. if ($messages = drupal_set_message()) {
  29. if ($type) {
  30. if ($clear_queue) {
  31. unset($_SESSION['messages'][$type]);
  32. }
  33. if (isset($messages[$type])) {
  34. return array($type => $messages[$type]);
  35. }
  36. }
  37. else {
  38. if ($clear_queue) {
  39. unset($_SESSION['messages']);
  40. }
  41. return $messages;
  42. }
  43. }
  44. return array();
  45. }