1 bootstrap.inc backdrop_set_message($message = NULL, $type = 'status', $repeat = TRUE)

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:

backdrop_set_message(t('An error occurred and processing did not complete.'), 'error');

@since 1.11.0 Added "info" as an acceptable $type parameter.

Parameters

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.

string $type: (optional) The type of the message. Defaults to 'status'. These values are supported:

  • 'status': Success messages that need to be displayed as a direct result of a user action. Shown once, on the page where the user action finishes. Usually shown with a green ✓ (check) icon.
  • 'info': Messages used for displaying generic information, or help text that may persist across various pages. Can be the result of a user action on another section of the site (like enabling a module or a certain setting for example). This type is to be used when the message is not an error, a warning, or a success message. Usually shown with a blue or gray "i" (info) icon.
  • 'warning': Cautionary advice messages, used to inform of a possible problem, or other unexpected behavior. Usually shown with a yellow or orange exclamation mark icon.
  • 'error': The opposite of success messages. Used to inform of problems that have been found, or have been caused as a direct result of a user action. Usually shown with a red "x" icon.

bool $repeat: (optional) If this is FALSE and the message is already set, then the message won't be repeated. Defaults to TRUE.

Return value

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 also

backdrop_get_messages()

theme_status_messages()

File

core/includes/bootstrap.inc, line 2412
Functions that need to be loaded on every Backdrop request.

Code

function backdrop_set_message($message = NULL, $type = 'status', $repeat = TRUE) {
  if ($message || $message === '0' || $message === 0) {
    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.
    backdrop_page_is_cacheable(FALSE);
  }

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