You are here

function drupal_set_message in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/includes/bootstrap.inc \drupal_set_message()

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|\Drupal\Component\Render\MarkupInterface $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, and each message is an associative array with the following format:

  • safe: Boolean indicating whether the message string has been marked as safe. Non-safe strings will be escaped automatically.
  • message: The message string.

So, the following is an example of the full return array structure:

array(
  'status' => array(
    array(
      'safe' => TRUE,
      'message' => 'A <em>safe</em> markup string.',
    ),
    array(
      'safe' => FALSE,
      'message' => "{$arbitrary_user_input} to escape.",
    ),
  ),
);

If there are no messages set, the function returns NULL.

See also

drupal_get_messages()

status-messages.html.twig

271 calls to drupal_set_message()
ActionFormBase::save in core/modules/action/src/ActionFormBase.php
Form submission handler for the 'save' action.
AdvancedSettingsForm::cacheSubmit in core/modules/views_ui/src/Form/AdvancedSettingsForm.php
Submission handler to clear the Views cache.
AggregatorController::feedRefresh in core/modules/aggregator/src/Controller/AggregatorController.php
Refreshes a feed, then redirects to the overview page.
AjaxFormBlock::submitForm in core/modules/system/tests/modules/ajax_forms_test/src/Plugin/Block/AjaxFormBlock.php
Form submission handler.
AjaxFormsTestValidationForm::submitForm in core/modules/system/tests/modules/ajax_forms_test/src/Form/AjaxFormsTestValidationForm.php
Form submission handler.

... See full list

1 string reference to 'drupal_set_message'
AggregatorPluginSettingsBaseTest.php in core/modules/aggregator/tests/src/Unit/Plugin/AggregatorPluginSettingsBaseTest.php
Contains \Drupal\Tests\aggregator\Unit\Plugin\AggregatorPluginSettingsBaseTest.

File

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

Code

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

    // Convert strings which are safe to the simplest Markup objects.
    if (!$message instanceof Markup && SafeMarkup::isSafe($message)) {
      $message = Markup::create((string) $message);
    }

    // Do not use strict type checking so that equivalent string and
    // MarkupInterface objects are detected.
    if ($repeat || !in_array($message, $_SESSION['messages'][$type])) {
      $_SESSION['messages'][$type][] = $message;
    }

    // Mark this page as being uncacheable.
    \Drupal::service('page_cache_kill_switch')
      ->trigger();
  }

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