3 namespace Drupal\Core\Messenger;
5 use Drupal\Component\Render\MarkupInterface;
6 use Drupal\Core\Render\Markup;
9 * Provides a LegacyMessenger implementation.
11 * This implementation is for handling messages in a backwards compatible way
12 * using core's previous $_SESSION storage method.
14 * You should not instantiate a new instance of this class directly. Instead,
15 * you should inject the "messenger" service into your own services or use
16 * \Drupal::messenger() in procedural functions.
18 * @see https://www.drupal.org/node/2774931
19 * @see https://www.drupal.org/node/2928994
21 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
22 * Use \Drupal\Core\Messenger\Messenger instead.
24 class LegacyMessenger implements MessengerInterface {
29 * Note: this property must remain static because it must behave in a
30 * persistent manner, similar to $_SESSION['messages']. Creating a new class
31 * each time would destroy any previously set messages.
35 protected static $messages;
40 public function addError($message, $repeat = FALSE) {
41 return $this->addMessage($message, static::TYPE_ERROR, $repeat);
47 public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
48 // Proxy to the Messenger service, if it exists.
49 if ($messenger = $this->getMessengerService()) {
50 return $messenger->addMessage($message, $type, $repeat);
53 if (!isset(static::$messages[$type])) {
54 static::$messages[$type] = [];
57 if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
58 $message = Markup::create((string) $message);
61 // Do not use strict type checking so that equivalent string and
62 // MarkupInterface objects are detected.
63 if ($repeat || !in_array($message, static::$messages[$type])) {
64 static::$messages[$type][] = $message;
73 public function addStatus($message, $repeat = FALSE) {
74 return $this->addMessage($message, static::TYPE_STATUS, $repeat);
80 public function addWarning($message, $repeat = FALSE) {
81 return $this->addMessage($message, static::TYPE_WARNING, $repeat);
87 public function all() {
88 // Proxy to the Messenger service, if it exists.
89 if ($messenger = $this->getMessengerService()) {
90 return $messenger->all();
93 return static::$messages;
97 * Returns the Messenger service.
99 * @return \Drupal\Core\Messenger\MessengerInterface|null
100 * The Messenger service.
102 protected function getMessengerService() {
103 // Use the Messenger service, if it exists.
104 if (\Drupal::hasService('messenger')) {
105 // Note: because the container has the potential to be rebuilt during
106 // requests, this service cannot be directly stored on this class.
107 /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
108 $messenger = \Drupal::service('messenger');
110 // Transfer any messages into the service.
111 if (isset(static::$messages)) {
112 foreach (static::$messages as $type => $messages) {
113 foreach ($messages as $message) {
114 // Force repeat to TRUE since this is merging existing messages to
115 // the Messenger service and would have already checked this prior.
116 $messenger->addMessage($message, $type, TRUE);
119 static::$messages = NULL;
125 // Otherwise, trigger an error.
126 @trigger_error('Adding or retrieving messages prior to the container being initialized was deprecated in Drupal 8.5.0 and this functionality will be removed before Drupal 9.0.0. Please report this usage at https://www.drupal.org/node/2928994.', E_USER_DEPRECATED);
128 // Prematurely creating $_SESSION['messages'] in this class' constructor
129 // causes issues when the container attempts to initialize its own session
130 // later down the road. This can only be done after it has been determined
131 // the Messenger service is not available (i.e. no container). It is also
132 // reasonable to assume that if the container becomes available in a
133 // subsequent request, a new instance of this class will be created and
134 // this code will never be reached. This is merely for BC purposes.
135 if (!isset(static::$messages)) {
136 // A "session" was already created, perhaps to simply allow usage of
137 // the previous method core used to store messages, use it.
138 if (isset($_SESSION)) {
139 if (!isset($_SESSION['messages'])) {
140 $_SESSION['messages'] = [];
142 static::$messages = &$_SESSION['messages'];
144 // Otherwise, just set an empty array.
146 static::$messages = [];
154 public function messagesByType($type) {
155 // Proxy to the Messenger service, if it exists.
156 if ($messenger = $this->getMessengerService()) {
157 return $messenger->messagesByType($type);
160 return static::$messages[$type];
166 public function deleteAll() {
167 // Proxy to the Messenger service, if it exists.
168 if ($messenger = $this->getMessengerService()) {
169 return $messenger->deleteAll();
172 $messages = static::$messages;
173 static::$messages = NULL;
180 public function deleteByType($type) {
181 // Proxy to the Messenger service, if it exists.
182 if ($messenger = $this->getMessengerService()) {
183 return $messenger->deleteByType($type);
186 $messages = static::$messages[$type];
187 unset(static::$messages[$type]);