diff --git a/core/core.services.yml b/core/core.services.yml index f9449bb..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: diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index 5570249..cfe0810 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -922,7 +922,7 @@ function watchdog($type, $message, array $variables = array(), $severity = WATCH * Use \Drupal::service('messenger')->addMessage() instead. */ function drupal_set_message($message = NULL, $type = 'status', $repeat = FALSE) { - /** @var \Drupal\Core\Messenger\MessengerInterface $messenger */ + /* @var \Drupal\Core\Messenger\MessengerInterface $messenger */ $messenger = \Drupal::service('messenger'); $messenger->addMessage($message, $type, $repeat); return $messenger->getMessages(); diff --git a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php index bf62c68..2022287 100644 --- a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php @@ -8,7 +8,7 @@ namespace Drupal\Core\EventSubscriber; use Drupal\Component\Utility\String; -use Drupal\Core\Messenger\MessengerTrait; +use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RouteSubscriberBase; use Symfony\Cmf\Component\Routing\RouteObjectInterface; @@ -18,7 +18,23 @@ * Provides a route subscriber which checks for invalid pattern variables. */ class SpecialAttributesRouteSubscriber extends RouteSubscriberBase { - use MessengerTrait; + + /** + * 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} @@ -39,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)); - $this->addMessage(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 index f7dac0b..13a5b7b 100644 --- a/core/lib/Drupal/Core/Messenger/Messenger.php +++ b/core/lib/Drupal/Core/Messenger/Messenger.php @@ -6,6 +6,7 @@ */ namespace Drupal\Core\Messenger; +use Drupal\Component\Utility\SafeMarkup; /** * Provides a session-based messenger. @@ -23,7 +24,10 @@ public function addMessage($message, $type = self::STATUS, $repeat = FALSE) { $_SESSION = array(); } if ($repeat || !array_key_exists('messages', $_SESSION) || !array_key_exists($type, $_SESSION['messages']) || !in_array($message, $_SESSION['messages'][$type])) { - $_SESSION['messages'][$type][] = $message; + $_SESSION['messages'][$type][] = array( + 'safe' => SafeMarkup::isSafe($message), + 'message' => $message, + ); } return $this; diff --git a/core/lib/Drupal/Core/Messenger/MessengerTrait.php b/core/lib/Drupal/Core/Messenger/MessengerTrait.php deleted file mode 100644 index 628e18e..0000000 --- a/core/lib/Drupal/Core/Messenger/MessengerTrait.php +++ /dev/null @@ -1,68 +0,0 @@ -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/Messenger/MessengerTraitTest.php b/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php deleted file mode 100644 index 8a7fa9c..0000000 --- a/core/tests/Drupal/Tests/Core/Messenger/MessengerTraitTest.php +++ /dev/null @@ -1,85 +0,0 @@ - '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)); - } - -}