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');
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:
bool $repeat: (optional) If this is FALSE and the message is already set, then the message won't be repeated. Defaults to FALSE.
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:
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.
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) && $message instanceof MarkupInterface) { $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; }
© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!includes!bootstrap.inc/function/drupal_set_message/8.1.x