Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 namespace Drupal\Core\Messenger;
|
Chris@14
|
4
|
Chris@14
|
5 use Drupal\Component\Render\MarkupInterface;
|
Chris@14
|
6 use Drupal\Core\Render\Markup;
|
Chris@14
|
7
|
Chris@14
|
8 /**
|
Chris@14
|
9 * Provides a LegacyMessenger implementation.
|
Chris@14
|
10 *
|
Chris@14
|
11 * This implementation is for handling messages in a backwards compatible way
|
Chris@14
|
12 * using core's previous $_SESSION storage method.
|
Chris@14
|
13 *
|
Chris@14
|
14 * You should not instantiate a new instance of this class directly. Instead,
|
Chris@14
|
15 * you should inject the "messenger" service into your own services or use
|
Chris@14
|
16 * \Drupal::messenger() in procedural functions.
|
Chris@14
|
17 *
|
Chris@14
|
18 * @see https://www.drupal.org/node/2774931
|
Chris@14
|
19 * @see https://www.drupal.org/node/2928994
|
Chris@14
|
20 *
|
Chris@14
|
21 * @deprecated in Drupal 8.5.0 and will be removed before Drupal 9.0.0.
|
Chris@14
|
22 * Use \Drupal\Core\Messenger\Messenger instead.
|
Chris@14
|
23 */
|
Chris@14
|
24 class LegacyMessenger implements MessengerInterface {
|
Chris@14
|
25
|
Chris@14
|
26 /**
|
Chris@14
|
27 * The messages.
|
Chris@14
|
28 *
|
Chris@14
|
29 * Note: this property must remain static because it must behave in a
|
Chris@14
|
30 * persistent manner, similar to $_SESSION['messages']. Creating a new class
|
Chris@14
|
31 * each time would destroy any previously set messages.
|
Chris@14
|
32 *
|
Chris@14
|
33 * @var array
|
Chris@14
|
34 */
|
Chris@14
|
35 protected static $messages;
|
Chris@14
|
36
|
Chris@14
|
37 /**
|
Chris@14
|
38 * {@inheritdoc}
|
Chris@14
|
39 */
|
Chris@14
|
40 public function addError($message, $repeat = FALSE) {
|
Chris@14
|
41 return $this->addMessage($message, static::TYPE_ERROR, $repeat);
|
Chris@14
|
42 }
|
Chris@14
|
43
|
Chris@14
|
44 /**
|
Chris@14
|
45 * {@inheritdoc}
|
Chris@14
|
46 */
|
Chris@14
|
47 public function addMessage($message, $type = self::TYPE_STATUS, $repeat = FALSE) {
|
Chris@14
|
48 // Proxy to the Messenger service, if it exists.
|
Chris@14
|
49 if ($messenger = $this->getMessengerService()) {
|
Chris@14
|
50 return $messenger->addMessage($message, $type, $repeat);
|
Chris@14
|
51 }
|
Chris@14
|
52
|
Chris@14
|
53 if (!isset(static::$messages[$type])) {
|
Chris@14
|
54 static::$messages[$type] = [];
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 if (!($message instanceof Markup) && $message instanceof MarkupInterface) {
|
Chris@14
|
58 $message = Markup::create((string) $message);
|
Chris@14
|
59 }
|
Chris@14
|
60
|
Chris@14
|
61 // Do not use strict type checking so that equivalent string and
|
Chris@14
|
62 // MarkupInterface objects are detected.
|
Chris@14
|
63 if ($repeat || !in_array($message, static::$messages[$type])) {
|
Chris@14
|
64 static::$messages[$type][] = $message;
|
Chris@14
|
65 }
|
Chris@14
|
66
|
Chris@14
|
67 return $this;
|
Chris@14
|
68 }
|
Chris@14
|
69
|
Chris@14
|
70 /**
|
Chris@14
|
71 * {@inheritdoc}
|
Chris@14
|
72 */
|
Chris@14
|
73 public function addStatus($message, $repeat = FALSE) {
|
Chris@14
|
74 return $this->addMessage($message, static::TYPE_STATUS, $repeat);
|
Chris@14
|
75 }
|
Chris@14
|
76
|
Chris@14
|
77 /**
|
Chris@14
|
78 * {@inheritdoc}
|
Chris@14
|
79 */
|
Chris@14
|
80 public function addWarning($message, $repeat = FALSE) {
|
Chris@14
|
81 return $this->addMessage($message, static::TYPE_WARNING, $repeat);
|
Chris@14
|
82 }
|
Chris@14
|
83
|
Chris@14
|
84 /**
|
Chris@14
|
85 * {@inheritdoc}
|
Chris@14
|
86 */
|
Chris@14
|
87 public function all() {
|
Chris@14
|
88 // Proxy to the Messenger service, if it exists.
|
Chris@14
|
89 if ($messenger = $this->getMessengerService()) {
|
Chris@14
|
90 return $messenger->all();
|
Chris@14
|
91 }
|
Chris@14
|
92
|
Chris@14
|
93 return static::$messages;
|
Chris@14
|
94 }
|
Chris@14
|
95
|
Chris@14
|
96 /**
|
Chris@14
|
97 * Returns the Messenger service.
|
Chris@14
|
98 *
|
Chris@14
|
99 * @return \Drupal\Core\Messenger\MessengerInterface|null
|
Chris@14
|
100 * The Messenger service.
|
Chris@14
|
101 */
|
Chris@14
|
102 protected function getMessengerService() {
|
Chris@14
|
103 // Use the Messenger service, if it exists.
|
Chris@14
|
104 if (\Drupal::hasService('messenger')) {
|
Chris@14
|
105 // Note: because the container has the potential to be rebuilt during
|
Chris@14
|
106 // requests, this service cannot be directly stored on this class.
|
Chris@14
|
107 /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */
|
Chris@14
|
108 $messenger = \Drupal::service('messenger');
|
Chris@14
|
109
|
Chris@14
|
110 // Transfer any messages into the service.
|
Chris@14
|
111 if (isset(static::$messages)) {
|
Chris@14
|
112 foreach (static::$messages as $type => $messages) {
|
Chris@14
|
113 foreach ($messages as $message) {
|
Chris@14
|
114 // Force repeat to TRUE since this is merging existing messages to
|
Chris@14
|
115 // the Messenger service and would have already checked this prior.
|
Chris@14
|
116 $messenger->addMessage($message, $type, TRUE);
|
Chris@14
|
117 }
|
Chris@14
|
118 }
|
Chris@14
|
119 static::$messages = NULL;
|
Chris@14
|
120 }
|
Chris@14
|
121
|
Chris@14
|
122 return $messenger;
|
Chris@14
|
123 }
|
Chris@14
|
124
|
Chris@14
|
125 // Otherwise, trigger an error.
|
Chris@14
|
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);
|
Chris@14
|
127
|
Chris@14
|
128 // Prematurely creating $_SESSION['messages'] in this class' constructor
|
Chris@14
|
129 // causes issues when the container attempts to initialize its own session
|
Chris@14
|
130 // later down the road. This can only be done after it has been determined
|
Chris@14
|
131 // the Messenger service is not available (i.e. no container). It is also
|
Chris@14
|
132 // reasonable to assume that if the container becomes available in a
|
Chris@14
|
133 // subsequent request, a new instance of this class will be created and
|
Chris@14
|
134 // this code will never be reached. This is merely for BC purposes.
|
Chris@14
|
135 if (!isset(static::$messages)) {
|
Chris@14
|
136 // A "session" was already created, perhaps to simply allow usage of
|
Chris@14
|
137 // the previous method core used to store messages, use it.
|
Chris@14
|
138 if (isset($_SESSION)) {
|
Chris@14
|
139 if (!isset($_SESSION['messages'])) {
|
Chris@14
|
140 $_SESSION['messages'] = [];
|
Chris@14
|
141 }
|
Chris@14
|
142 static::$messages = &$_SESSION['messages'];
|
Chris@14
|
143 }
|
Chris@14
|
144 // Otherwise, just set an empty array.
|
Chris@14
|
145 else {
|
Chris@14
|
146 static::$messages = [];
|
Chris@14
|
147 }
|
Chris@14
|
148 }
|
Chris@14
|
149 }
|
Chris@14
|
150
|
Chris@14
|
151 /**
|
Chris@14
|
152 * {@inheritdoc}
|
Chris@14
|
153 */
|
Chris@14
|
154 public function messagesByType($type) {
|
Chris@14
|
155 // Proxy to the Messenger service, if it exists.
|
Chris@14
|
156 if ($messenger = $this->getMessengerService()) {
|
Chris@14
|
157 return $messenger->messagesByType($type);
|
Chris@14
|
158 }
|
Chris@14
|
159
|
Chris@14
|
160 return static::$messages[$type];
|
Chris@14
|
161 }
|
Chris@14
|
162
|
Chris@14
|
163 /**
|
Chris@14
|
164 * {@inheritdoc}
|
Chris@14
|
165 */
|
Chris@14
|
166 public function deleteAll() {
|
Chris@14
|
167 // Proxy to the Messenger service, if it exists.
|
Chris@14
|
168 if ($messenger = $this->getMessengerService()) {
|
Chris@14
|
169 return $messenger->deleteAll();
|
Chris@14
|
170 }
|
Chris@14
|
171
|
Chris@14
|
172 $messages = static::$messages;
|
Chris@14
|
173 static::$messages = NULL;
|
Chris@14
|
174 return $messages;
|
Chris@14
|
175 }
|
Chris@14
|
176
|
Chris@14
|
177 /**
|
Chris@14
|
178 * {@inheritdoc}
|
Chris@14
|
179 */
|
Chris@14
|
180 public function deleteByType($type) {
|
Chris@14
|
181 // Proxy to the Messenger service, if it exists.
|
Chris@14
|
182 if ($messenger = $this->getMessengerService()) {
|
Chris@17
|
183 return $messenger->deleteByType($type);
|
Chris@14
|
184 }
|
Chris@14
|
185
|
Chris@14
|
186 $messages = static::$messages[$type];
|
Chris@14
|
187 unset(static::$messages[$type]);
|
Chris@14
|
188 return $messages;
|
Chris@14
|
189 }
|
Chris@14
|
190
|
Chris@14
|
191 }
|