function drupal_set_message

8.x bootstrap.inc drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE)

Sets a message to display to the user.

Messages are stored in a session variable and displayed in the page template via the $messages theme variable.

Example usage:

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

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 message's type. Defaults to 'status'. These values are supported:

  • 'status'
  • 'warning'
  • 'error'

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

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

drupal_get_messages()

theme_status_messages()

113 calls to drupal_set_message()
aggregator_refresh in drupal/core/modules/aggregator/aggregator.module
Checks a news feed for new items.
ajax_forms_test_validation_form_callback in drupal/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
Ajax form callback: Selects the 'drivertext' element of the validation form.
ajax_forms_test_validation_form_submit in drupal/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
Form submission handler for ajax_forms_test_validation_form().
ajax_forms_test_validation_number_form_callback in drupal/core/modules/system/tests/modules/ajax_forms_test/ajax_forms_test.module
Ajax form callback: Selects the 'drivernumber' element of the validation form.
authorize.php in drupal/core/authorize.php
Administrative script for running authorized file operations.

... See full list

File

drupal/core/includes/bootstrap.inc, line 1623
Functions that need to be loaded on every Drupal request.

Code

function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) {
  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;
}