diff --git a/core/core.services.yml b/core/core.services.yml index 4c1025d..ded185d 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -883,3 +883,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 635ddb8..a78a1b6 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -874,7 +874,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 @@ -893,23 +893,17 @@ 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(); - } + /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ + $messenger = \Drupal::service('messenger'); - if ($repeat || !in_array($message, $_SESSION['messages'][$type])) { - $_SESSION['messages'][$type][] = $message; - } - - // Mark this page as being uncacheable. - drupal_page_is_cacheable(FALSE); - } + $messenger->addMessage($message, $type, $repeat); - // Messages not set when DB connection fails. - return isset($_SESSION['messages']) ? $_SESSION['messages'] : NULL; + return $messenger->getMessages(); } /** @@ -936,12 +930,24 @@ 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()) { if ($type) { if ($clear_queue) { - unset($_SESSION['messages'][$type]); + $messenger->deleteMessagesByType($type); } if (isset($messages[$type])) { return array($type => $messages[$type]); @@ -949,7 +955,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..bf62c68 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\MessengerTrait; use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -17,6 +18,7 @@ * Provides a route subscriber which checks for invalid pattern variables. */ class SpecialAttributesRouteSubscriber extends RouteSubscriberBase { + use MessengerTrait; /** * {@inheritdoc} @@ -37,7 +39,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->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..f7dac0b --- /dev/null +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -0,0 +1,64 @@ +getMessenger()->addMessage($message, $type, $repeat); + return $this; + } + + /** + * Gets the messenger service. + * + * @return \Drupal\Core\Messenger\MessengerInterface + * The messenger service. + */ + protected function getMessenger() { + if (!$this->messenger) { + $this->messenger = \Drupal::service('messenger'); + } + + return $this->messenger; + } + + /** + * Sets the messenger service to use. + * + * @param \Drupal\Core\Messenger\MessengerInterface $messenger + * The messenger service. + * + * @return $this + */ + public function setMessenger(MessengerInterface $messenger) { + $this->messenger = $messenger; + + return $this; + } + + +} diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php index cdab44f..9ca8902 100644 --- a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php +++ b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php @@ -34,6 +34,8 @@ protected function setUp() { parent::setUp(); $this->specialAttributesRouteSubscriber = new SpecialAttributesRouteSubscriber(); + $messenger = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->specialAttributesRouteSubscriber->setMessenger($messenger); } /** @@ -112,8 +114,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..1a042bc --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Messenger/MessengerTest.php @@ -0,0 +1,99 @@ + '', + '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($message_a, $message_a), + $type_b => array($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($message_b), + )); + + // Test deleting all messages. + $this->assertEquals($this->messenger->deleteMessages(), $this->messenger); + $this->assertEquals($this->messenger->getMessages(), array()); + } + +} diff --git a/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php b/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php new file mode 100644 index 0000000..8a7fa9c --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php @@ -0,0 +1,85 @@ + 'Messenger trait', + 'description' => 'Tests the messenger trait.', + 'group' => 'Messenger', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + $this->messengerTrait = $this->getObjectForTrait('\Drupal\Core\Messenger\MessengerTrait'); + + $this->reflection = new \ReflectionClass(get_class($this->messengerTrait)); + } + + /** + * @covers ::addMessage + */ + public function testAddMessage() { + $stub = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->messengerTrait->setMessenger($stub); + + $message_string = 'Beep boop'; + + $stub->expects($this->once()) + ->method('addMessage') + ->with($this->equalTo($message_string)); + + $method = $this->reflection->getMethod('addMessage'); + $method->setAccessible(TRUE); + + $this->assertEquals($this->messengerTrait, $method->invoke($this->messengerTrait, $message_string)); + } + + /** + * @covers ::getMessenger + */ + public function testGetMessenger() { + $stub = $this->getMock('\Drupal\Core\Messenger\MessengerInterface'); + $this->messengerTrait->setMessenger($stub); + + $method = $this->reflection->getMethod('getMessenger'); + $method->setAccessible(TRUE); + + $this->assertSame($stub, $method->invoke($this->messengerTrait)); + } + +}