diff --git a/core/core.services.yml b/core/core.services.yml index 562d64f..d65e113 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -545,6 +545,7 @@ services: - { name: event_subscriber } route_special_attributes_subscriber: class: Drupal\Core\EventSubscriber\SpecialAttributesRouteSubscriber + arguments: ['@messenger'] tags: - { name: event_subscriber } route_http_method_subscriber: @@ -888,3 +889,5 @@ services: arguments: ['@module_handler'] tags: - { name: mime_type_guesser } + messenger: + class: Drupal\Core\Messenger\Messenger diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 27494e7..cfe0810 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -878,7 +878,7 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH * * @param 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 + * consistency with other messages it should begin with a capital letter and * end with a period. * @param string $type * (optional) The message's type. Defaults to 'status'. These values are @@ -916,26 +916,16 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH * * @see drupal_get_messages() * @see theme_status_messages() + * + * + * @deprecated Deprecated as of Drupal 8.0. + * Use \Drupal::service('messenger')->addMessage() instead. */ 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][] = array( - 'safe' => SafeMarkup::isSafe($message), - 'message' => $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; + /* @var \Drupal\Core\Messenger\MessengerInterface $messenger */ + $messenger = \Drupal::service('messenger'); + $messenger->addMessage($message, $type, $repeat); + return $messenger->getMessages(); } /** @@ -962,9 +952,22 @@ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) * * @see drupal_set_message() * @see theme_status_messages() + * + * + * @deprecated Deprecated as of Drupal 8.0. + * Use \Drupal::service('messenger')->getMessages() or + * \Drupal::service('messenger')->getMessagesByType() instead. */ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { - if ($messages = drupal_set_message()) { + // Workaround for Drush. + if (!\Drupal::hasService('messenger')) { + return array(); + } + /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ + $messenger = \Drupal::service('messenger'); + + if ($messages = $messenger->getMessages()) { + foreach ($messages as $message_type => $message_typed_messages) { foreach ($message_typed_messages as $key => $message) { if ($message['safe']) { @@ -975,7 +978,7 @@ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { } if ($type) { if ($clear_queue) { - unset($_SESSION['messages'][$type]); + $messenger->deleteMessagesByType($type); } if (isset($messages[$type])) { return array($type => $messages[$type]); @@ -983,7 +986,7 @@ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { } else { if ($clear_queue) { - unset($_SESSION['messages']); + $messenger->deleteMessages(); } return $messages; } diff --git a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php index 8556561..2022287 100644 --- a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php @@ -8,6 +8,7 @@ namespace Drupal\Core\EventSubscriber; use Drupal\Component\Utility\String; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -19,6 +20,23 @@ class SpecialAttributesRouteSubscriber extends RouteSubscriberBase { /** + * The messenger service. + * + * @var \Drupal\Core\Messenger\MessengerInterface + */ + protected $messenger; + + /** + * Creates a new special attributes route subscriber. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. + */ + public function __construct(MessengerInterface $messenger) { + $this->messenger = $messenger; + } + + /** * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection) { @@ -37,7 +55,7 @@ protected function alterRoutes(RouteCollection $collection) { foreach ($collection->all() as $route) { if ($not_allowed_variables = array_intersect($route->compile()->getVariables(), $special_variables)) { $placeholders = array('@variables' => implode(', ', $not_allowed_variables)); - drupal_set_message(String::format('The following variables are reserved names by drupal: @variables', $placeholders)); + $this->messenger->addMessage(String::format('The following variables are reserved names by drupal: @variables', $placeholders)); watchdog('error', 'The following variables are reserved names by drupal: @variables', $placeholders); return FALSE; } diff --git a/core/lib/Drupal/Core/Messenger/Messenger.php b/core/lib/Drupal/Core/Messenger/Messenger.php new file mode 100644 index 0000000..13a5b7b --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -0,0 +1,68 @@ + SafeMarkup::isSafe($message), + 'message' => $message, + ); + } + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getMessages() { + return isset($_SESSION['messages']) ? $_SESSION['messages'] : array(); + } + + /** + * {@inheritdoc} + */ + public function getMessagesByType($type) { + return isset($_SESSION['messages']) && isset($_SESSION['messages'][$type]) ? $_SESSION['messages'][$type] : array(); + } + + /** + * {@inheritdoc} + */ + public function deleteMessages() { + unset($_SESSION['messages']); + + return $this; + } + + /** + * {@inheritdoc} + */ + public function deleteMessagesByType($type) { + unset($_SESSION['messages'][$type]); + + return $this; + } + +} diff --git a/core/lib/Drupal/Core/Messenger/MessengerInterface.php b/core/lib/Drupal/Core/Messenger/MessengerInterface.php new file mode 100644 index 0000000..044b16c --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/MessengerInterface.php @@ -0,0 +1,81 @@ +specialAttributesRouteSubscriber = new SpecialAttributesRouteSubscriber(); + $messenger = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->specialAttributesRouteSubscriber = new SpecialAttributesRouteSubscriber($messenger); } /** @@ -112,8 +113,4 @@ public function testOnRouteBuildingInvalidVariables(Route $route) { function watchdog($type, $message, array $args = array()) { } } - if (!function_exists('drupal_set_message')) { - function drupal_set_message($type = NULL, $message = '') { - } - } } diff --git a/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php b/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php new file mode 100644 index 0000000..ccdd46d --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php @@ -0,0 +1,115 @@ + '', + 'name' => '\Drupal\Core\Messenger\Messenger unit test', + 'group' => 'Messenger', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->messenger = new Messenger(); + + $this->existingSession = isset($_SESSION) ? $_SESSION : NULL; + $_SESSION = array(); + } + + /** + * {@inheritdoc} + */ + public function tearDown() { + if ($this->existingSession !== NULL) { + $_SESSION = $this->existingSession; + } + else { + unset($_SESSION); + } + } + + /** + * Tests the messenger. + */ + public function testMessenger() { + $message_a = $this->randomName(); + $type_a = $this->randomName(); + $message_b = $this->randomName(); + $type_b = $this->randomName(); + + // Test that if there are no messages, the default is an empty array. + $this->assertEquals($this->messenger->getMessages(), array()); + + // Test that adding a message returns the messenger and that the message can + // be retrieved. + $this->assertEquals($this->messenger->addMessage($message_a, $type_a), $this->messenger); + $this->messenger->addMessage($message_a, $type_a); + $this->messenger->addMessage($message_a, $type_a, TRUE); + $this->messenger->addMessage($message_b, $type_b, TRUE); + $this->assertEquals($this->messenger->getMessages(), array( + $type_a => array( + array( + 'safe' => FALSE, + 'message' => $message_a, + ), + array( + 'safe' => FALSE, + 'message' => $message_a, + ), + array( + 'safe' => FALSE, + 'message' => $message_a, + ), + ), + $type_b => array( + array('safe' => FALSE, 'message' => $message_b), + ), + ) + ); + + // Test deleting messages of a certain type. + $this->assertEquals($this->messenger->deleteMessagesByType($type_a), $this->messenger); + $this->assertEquals($this->messenger->getMessages(), array( + $type_b => array(array('safe' => FALSE, 'message' => $message_b)), + )); + + // Test deleting all messages. + $this->assertEquals($this->messenger->deleteMessages(), $this->messenger); + $this->assertEquals($this->messenger->getMessages(), array()); + } + +}