summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathaniel Catchpole2013-10-18 09:54:56 +0100
committerNathaniel Catchpole2013-10-18 09:54:56 +0100
commit784cebbe5a3ef9caa2211bca9a0ac1e510c6bb7a (patch)
tree76f5b2195fbc0193ab32ab939e9f23d10e81ad8f
parent25f081af065a28bb7482e62188d874c41981b4b9 (diff)
Issue #2098795 by tim.plunkett, Sean Charles, tstoeckler, dawehner, larowlan, damiankloip: Create Base Class for RouteSubscriber Class.8.0-alpha4
-rw-r--r--core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php22
-rw-r--r--core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php32
-rw-r--r--core/lib/Drupal/Core/Routing/RouteSubscriberBase.php74
-rw-r--r--core/modules/block/lib/Drupal/block/Routing/RouteSubscriber.php27
-rw-r--r--core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php18
-rw-r--r--core/modules/field_ui/lib/Drupal/field_ui/Routing/RouteSubscriber.php20
-rw-r--r--core/modules/image/lib/Drupal/image/EventSubscriber/RouteSubscriber.php31
-rw-r--r--core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php28
-rw-r--r--core/modules/search/lib/Drupal/search/Routing/SearchRouteSubscriber.php24
-rw-r--r--core/modules/system/lib/Drupal/system/Routing/RouteSubscriber.php23
-rw-r--r--core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php18
-rw-r--r--core/modules/system/tests/modules/form_test/lib/Drupal/form_test/EventSubscriber/FormTestEventSubscriber.php17
-rw-r--r--core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php35
-rw-r--r--core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayPageTest.php2
-rw-r--r--core/modules/views/tests/Drupal/views/Tests/EventSubscriber/RouteSubscriberTest.php16
-rw-r--r--core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php2
-rw-r--r--core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php8
17 files changed, 166 insertions, 231 deletions
diff --git a/core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php
index 5bb903e..49f1a12 100644
--- a/core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/ModuleRouteSubscriber.php
@@ -8,14 +8,13 @@
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Extension\ModuleHandlerInterface;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-use Drupal\Core\Routing\RoutingEvents;
+use Drupal\Core\Routing\RouteSubscriberBase;
+use Symfony\Component\Routing\RouteCollection;
/**
* A route subscriber to remove routes that depend on modules being enabled.
*/
-class ModuleRouteSubscriber implements EventSubscriberInterface {
+class ModuleRouteSubscriber extends RouteSubscriberBase {
/**
* The module handler.
@@ -37,20 +36,7 @@ class ModuleRouteSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::ALTER] = 'removeRoutes';
- return $events;
- }
-
- /**
- * Removes any routes that have an unmet module dependency.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
- */
- public function removeRoutes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
-
+ protected function alterRoutes(RouteCollection $collection, $module) {
foreach ($collection as $name => $route) {
if ($route->hasRequirement('_module_dependencies')) {
$modules = $route->getRequirement('_module_dependencies');
diff --git a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php
index 22e82d2..39f9ceb 100644
--- a/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/SpecialAttributesRouteSubscriber.php
@@ -9,25 +9,19 @@ namespace Drupal\Core\EventSubscriber;
use Drupal\Component\Utility\String;
use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Routing\RouteCollection;
/**
* Provides a route subscriber which checks for invalid pattern variables.
*/
-class SpecialAttributesRouteSubscriber implements EventSubscriberInterface {
+class SpecialAttributesRouteSubscriber extends RouteSubscriberBase {
/**
- * Checks for invalid pattern variables.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The event containing the build routes.
- *
- * @throws \InvalidArgumentException
- * Thrown when a reserved variable was used as route variable.
+ * {@inheritdoc}
*/
- public function onRouteBuilding(RouteBuildEvent $event) {
+ protected function alterRoutes(RouteCollection $collection, $module) {
$special_variables = array(
'_account',
'system_path',
@@ -41,7 +35,7 @@ class SpecialAttributesRouteSubscriber implements EventSubscriberInterface {
'_form',
);
- foreach ($event->getRouteCollection()->all() as $route) {
+ 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));
@@ -53,11 +47,17 @@ class SpecialAttributesRouteSubscriber implements EventSubscriberInterface {
}
/**
- * {@inheritdoc}
+ * Delegates the route altering to self::alterRoutes().
+ *
+ * @param \Drupal\Core\Routing\RouteBuildEvent $event
+ * The route build event.
+ *
+ * @return bool
+ * Returns TRUE if the variables were succesfully replaced, otherwise FALSE.
*/
- static function getSubscribedEvents() {
- $events[RoutingEvents::ALTER][] = 'onRouteBuilding';
- return $events;
+ public function onAlterRoutes(RouteBuildEvent $event) {
+ $collection = $event->getRouteCollection();
+ return $this->alterRoutes($collection, $event->getModule());
}
}
diff --git a/core/lib/Drupal/Core/Routing/RouteSubscriberBase.php b/core/lib/Drupal/Core/Routing/RouteSubscriberBase.php
new file mode 100644
index 0000000..615a820
--- /dev/null
+++ b/core/lib/Drupal/Core/Routing/RouteSubscriberBase.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Core\Routing\RouteSubscriberBase
+ */
+
+namespace Drupal\Core\Routing;
+
+use Drupal\Core\Routing\RoutingEvents;
+use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Routing\RouteCollection;
+
+/**
+ * Provides a base implementation for RouteSubscriber.
+ */
+abstract class RouteSubscriberBase implements EventSubscriberInterface {
+
+ /**
+ * Provides new routes by adding them to the collection.
+ *
+ * Subclasses should use this method and add \Symfony\Component\Routing\Route
+ * objects with $collection->add($route);.
+ *
+ * @param \Symfony\Component\Routing\RouteCollection $collection
+ * The route collection for adding routes.
+ */
+ protected function routes(RouteCollection $collection) {
+ }
+
+ /**
+ * Alters existing routes for a specific collection.
+ *
+ * @param \Symfony\Component\Routing\RouteCollection $collection
+ * The route collection for adding routes.
+ * @param string $module
+ * The module these routes belong to. For dynamically added routes, the
+ * module name will be 'dynamic_routes'.
+ */
+ protected function alterRoutes(RouteCollection $collection, $module) {
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function getSubscribedEvents() {
+ $events[RoutingEvents::DYNAMIC] = 'onDynamicRoutes';
+ $events[RoutingEvents::ALTER] = 'onAlterRoutes';
+ return $events;
+ }
+
+ /**
+ * Delegates the route gathering to self::routes().
+ *
+ * @param \Drupal\Core\Routing\RouteBuildEvent $event
+ * The route build event.
+ */
+ public function onDynamicRoutes(RouteBuildEvent $event) {
+ $collection = $event->getRouteCollection();
+ $this->routes($collection);
+ }
+
+ /**
+ * Delegates the route altering to self::alterRoutes().
+ *
+ * @param \Drupal\Core\Routing\RouteBuildEvent $event
+ * The route build event.
+ */
+ public function onAlterRoutes(RouteBuildEvent $event) {
+ $collection = $event->getRouteCollection();
+ $this->alterRoutes($collection, $event->getModule());
+ }
+
+}
diff --git a/core/modules/block/lib/Drupal/block/Routing/RouteSubscriber.php b/core/modules/block/lib/Drupal/block/Routing/RouteSubscriber.php
index 843e778..ce49b77 100644
--- a/core/modules/block/lib/Drupal/block/Routing/RouteSubscriber.php
+++ b/core/modules/block/lib/Drupal/block/Routing/RouteSubscriber.php
@@ -7,36 +7,19 @@
namespace Drupal\block\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
-use Symfony\Component\DependencyInjection\ContainerInterface;
+use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for various block pages.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
- * Implements EventSubscriberInterface::getSubscribedEvents().
+ * {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'routes';
- return $events;
- }
-
- /**
- * Generates dynamic routes for various block pages.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
- *
- * @return \Symfony\Component\Routing\RouteCollection
- * The route collection that contains the new dynamic route.
- */
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
foreach (list_themes(TRUE) as $key => $theme) {
// The block entity listing page.
$route = new Route(
diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php b/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php
index cb4858c..ae4a55c 100644
--- a/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php
+++ b/core/modules/content_translation/lib/Drupal/content_translation/Routing/ContentTranslationRouteSubscriber.php
@@ -7,16 +7,15 @@
namespace Drupal\content_translation\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Entity\EntityManager;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for entity translation routes.
*/
-class ContentTranslationRouteSubscriber implements EventSubscriberInterface {
+class ContentTranslationRouteSubscriber extends RouteSubscriberBase {
/**
* The entity type manager.
@@ -38,16 +37,7 @@ class ContentTranslationRouteSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'routes';
- return $events;
- }
-
- /**
- * Adds routes for entity translations.
- */
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
if ($entity_info['translatable'] && isset($entity_info['translation'])) {
$path = '/' . str_replace($entity_info['menu_path_wildcard'], '{' . $entity_type . '}', $entity_info['menu_base_path']) . '/translations';
diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Routing/RouteSubscriber.php b/core/modules/field_ui/lib/Drupal/field_ui/Routing/RouteSubscriber.php
index 456e1fd..6e3b003 100644
--- a/core/modules/field_ui/lib/Drupal/field_ui/Routing/RouteSubscriber.php
+++ b/core/modules/field_ui/lib/Drupal/field_ui/Routing/RouteSubscriber.php
@@ -7,16 +7,15 @@
namespace Drupal\field_ui\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
-use Symfony\Component\Routing\Route;
use Drupal\Core\Entity\EntityManager;
+use Drupal\Core\Routing\RouteSubscriberBase;
+use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Field UI routes.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
* The entity type manager
@@ -38,16 +37,7 @@ class RouteSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'routes';
- return $events;
- }
-
- /**
- * Adds routes for the Field UI.
- */
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
foreach ($this->manager->getDefinitions() as $entity_type => $entity_info) {
$defaults = array();
if ($entity_info['fieldable'] && isset($entity_info['route_base_path'])) {
diff --git a/core/modules/image/lib/Drupal/image/EventSubscriber/RouteSubscriber.php b/core/modules/image/lib/Drupal/image/EventSubscriber/RouteSubscriber.php
index 2524522..59fbb75 100644
--- a/core/modules/image/lib/Drupal/image/EventSubscriber/RouteSubscriber.php
+++ b/core/modules/image/lib/Drupal/image/EventSubscriber/RouteSubscriber.php
@@ -7,38 +7,23 @@
namespace Drupal\image\EventSubscriber;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Defines a route subscriber to register a url for serving image styles.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
- return $events;
- }
-
- /**
- * Registers dynamic routes for image styles.
- *
- * Generate image derivatives of publicly available files. If clean URLs are
- * disabled, image derivatives will always be served through the menu system.
- * If clean URLs are enabled and the image derivative already exists, PHP will
- * be bypassed.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
- */
- public function dynamicRoutes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
-
+ protected function routes(RouteCollection $collection) {
+ // Generate image derivatives of publicly available files. If clean URLs are
+ // disabled image derivatives will always be served through the menu system.
+ // If clean URLs are enabled and the image derivative already exists, PHP
+ // will be bypassed.
$directory_path = file_stream_wrapper_get_instance_by_scheme('public')->getDirectoryPath();
$route = new Route('/' . $directory_path . '/styles/{image_style}/{scheme}',
diff --git a/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php b/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php
index 325f472..34fd75d 100644
--- a/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php
+++ b/core/modules/rest/lib/Drupal/rest/EventSubscriber/RouteSubscriber.php
@@ -1,22 +1,21 @@
<?php
/**
- * Definition of \Drupal\rest\EventSubscriber\RouteSubscriber.
+ * @file
+ * Contains \Drupal\rest\EventSubscriber\RouteSubscriber.
*/
namespace Drupal\rest\EventSubscriber;
use Drupal\Core\Config\ConfigFactory;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\rest\Plugin\Type\ResourcePluginManager;
-
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for REST-style routes.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
* The plugin manager for REST plugins.
@@ -46,14 +45,9 @@ class RouteSubscriber implements EventSubscriberInterface {
}
/**
- * Adds routes to enabled REST resources.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
+ * {@inheritdoc}
*/
- public function dynamicRoutes(RouteBuildEvent $event) {
-
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
$enabled_resources = $this->config->get('rest.settings')->load()->get('resources');
// Iterate over all enabled resource plugins.
@@ -88,12 +82,4 @@ class RouteSubscriber implements EventSubscriberInterface {
}
}
- /**
- * Implements EventSubscriberInterface::getSubscribedEvents().
- */
- static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
- return $events;
- }
}
-
diff --git a/core/modules/search/lib/Drupal/search/Routing/SearchRouteSubscriber.php b/core/modules/search/lib/Drupal/search/Routing/SearchRouteSubscriber.php
index 5564ba4..0061d83 100644
--- a/core/modules/search/lib/Drupal/search/Routing/SearchRouteSubscriber.php
+++ b/core/modules/search/lib/Drupal/search/Routing/SearchRouteSubscriber.php
@@ -2,21 +2,20 @@
/**
* @file
- * Contains Drupal\search\Routing\SearchRouteSubscriber
+ * Contains \Drupal\search\Routing\SearchRouteSubscriber.
*/
namespace Drupal\search\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\search\SearchPluginManager;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for search.
*/
-class SearchRouteSubscriber implements EventSubscriberInterface {
+class SearchRouteSubscriber extends RouteSubscriberBase {
/**
* The search plugin manager.
@@ -38,20 +37,7 @@ class SearchRouteSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'routes';
- return $events;
- }
-
- /**
- * Adds routes for search.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
- */
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
-
+ protected function routes(RouteCollection $collection) {
foreach ($this->searchManager->getActiveDefinitions() as $plugin_id => $search_info) {
$path = 'search/' . $search_info['path'] . '/{keys}';
$defaults = array(
diff --git a/core/modules/system/lib/Drupal/system/Routing/RouteSubscriber.php b/core/modules/system/lib/Drupal/system/Routing/RouteSubscriber.php
index d9ef34d..de5304e 100644
--- a/core/modules/system/lib/Drupal/system/Routing/RouteSubscriber.php
+++ b/core/modules/system/lib/Drupal/system/Routing/RouteSubscriber.php
@@ -7,32 +7,19 @@
namespace Drupal\system\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Provides dynamic routes for theme administration.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
- * Implements EventSubscriberInterface::getSubscribedEvents().
+ * {@inheritdoc}
*/
- static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'createSystemThemeRoutes';
- return $events;
- }
-
- /**
- * Adds dynamic system theme routes.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
- */
- public function createSystemThemeRoutes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
foreach (list_themes() as $theme) {
if (!empty($theme->status)) {
$route = new Route('admin/appearance/settings/' . $theme->name, array(
diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php
index 8834b4a..ae09b55 100644
--- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php
+++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/Routing/RouteSubscriber.php
@@ -7,29 +7,19 @@
namespace Drupal\entity_test\Routing;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\Route;
+use Symfony\Component\Routing\RouteCollection;
/**
* Subscriber for Entity Test routes.
*/
-class RouteSubscriber implements EventSubscriberInterface {
+class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'routes';
- return $events;
- }
-
- /**
- * Adds routes for the Entity Test.
- */
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
+ protected function routes(RouteCollection $collection) {
$types = entity_test_entity_types();
$types[] = 'entity_test_render';
diff --git a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/EventSubscriber/FormTestEventSubscriber.php b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/EventSubscriber/FormTestEventSubscriber.php
index 11e5a7a..552398b 100644
--- a/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/EventSubscriber/FormTestEventSubscriber.php
+++ b/core/modules/system/tests/modules/form_test/lib/Drupal/form_test/EventSubscriber/FormTestEventSubscriber.php
@@ -7,18 +7,18 @@
namespace Drupal\form_test\EventSubscriber;
-use Drupal\Core\Routing\RouteBuildEvent;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\Core\Routing\RoutingEvents;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Symfony\Component\Routing\Route;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
+use Symfony\Component\Routing\RouteCollection;
/**
* Test event subscriber to add new attributes to the request.
*/
-class FormTestEventSubscriber implements EventSubscriberInterface {
+class FormTestEventSubscriber extends RouteSubscriberBase {
/**
* The module handler.
@@ -51,18 +51,15 @@ class FormTestEventSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array('onKernelRequest');
- $events[RoutingEvents::DYNAMIC] = 'routes';
-
+ $events[RoutingEvents::DYNAMIC] = 'onDynamicRoutes';
+ $events[RoutingEvents::ALTER] = 'onAlterRoutes';
return $events;
}
/**
- * Adds routes for the Form Tests.
+ * {@inheritdoc}
*/
- public function routes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
- $defaults = array();
-
+ protected function routes(RouteCollection $collection) {
if ($this->moduleHandler->moduleExists('node')) {
$route = new Route(
"form-test/two-instances-of-same-form",
diff --git a/core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php b/core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php
index 2ab8cdb..f3320db 100644
--- a/core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php
+++ b/core/modules/views/lib/Drupal/views/EventSubscriber/RouteSubscriber.php
@@ -8,15 +8,13 @@
namespace Drupal\views\EventSubscriber;
use Drupal\Component\Utility\MapArray;
-use Drupal\Component\Utility\NestedArray;
use Drupal\Core\DestructableInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
-use Drupal\Core\Routing\RouteBuildEvent;
-use Drupal\Core\Routing\RoutingEvents;
+use Drupal\Core\Routing\RouteSubscriberBase;
use Drupal\views\Plugin\views\display\DisplayRouterInterface;
use Drupal\views\ViewExecutable;
-use Symfony\Component\EventDispatcher\EventSubscriberInterface;
+use Symfony\Component\Routing\RouteCollection;
/**
* Builds up the routes of all views.
@@ -27,7 +25,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
*
* @see \Drupal\views\Plugin\views\display\PathPluginBase
*/
-class RouteSubscriber implements EventSubscriberInterface, DestructableInterface {
+class RouteSubscriber extends RouteSubscriberBase implements DestructableInterface {
/**
* Stores a list of view,display IDs which haven't be used in the alter event.
@@ -78,15 +76,6 @@ class RouteSubscriber implements EventSubscriberInterface, DestructableInterface
}
/**
- * {@inheritdoc}
- */
- public static function getSubscribedEvents() {
- $events[RoutingEvents::DYNAMIC] = 'dynamicRoutes';
- $events[RoutingEvents::ALTER] = 'alterRoutes';
- return $events;
- }
-
- /**
* Gets all the views and display IDs using a route.
*/
protected function getViewsDisplayIDsWithRoute() {
@@ -106,14 +95,9 @@ class RouteSubscriber implements EventSubscriberInterface, DestructableInterface
}
/**
- * Adds routes defined by all views.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
+ * {@inheritdoc}
*/
- public function dynamicRoutes(RouteBuildEvent $event) {
- $collection = $event->getRouteCollection();
-
+ protected function routes(RouteCollection $collection) {
foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorageController->load($view_id);
@@ -134,12 +118,9 @@ class RouteSubscriber implements EventSubscriberInterface, DestructableInterface
}
/**
- * Alters existing routes.
- *
- * @param \Drupal\Core\Routing\RouteBuildEvent $event
- * The route building event.
+ * {@inheritdoc}
*/
- public function alterRoutes(RouteBuildEvent $event) {
+ protected function alterRoutes(RouteCollection $collection, $module) {
foreach ($this->getViewsDisplayIDsWithRoute() as $pair) {
list($view_id, $display_id) = explode('.', $pair);
$view = $this->viewStorageController->load($view_id);
@@ -149,7 +130,7 @@ class RouteSubscriber implements EventSubscriberInterface, DestructableInterface
if ($display instanceof DisplayRouterInterface) {
// If the display returns TRUE a route item was found, so it does not
// have to be added.
- $view_route_names = $display->alterRoutes($event->getRouteCollection());
+ $view_route_names = $display->alterRoutes($collection);
$this->viewRouteNames += $view_route_names;
foreach ($view_route_names as $id_display => $route_name) {
unset($this->viewsDisplayPairs[$id_display]);
diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayPageTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayPageTest.php
index f420943..3e5b590 100644
--- a/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayPageTest.php
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/DisplayPageTest.php
@@ -95,7 +95,7 @@ class DisplayPageTest extends ViewUnitTestBase {
public function testPageRouterItems() {
$subscriber = new RouteSubscriber($this->container->get('entity.manager'), $this->container->get('state'));
$collection = new RouteCollection();
- $subscriber->dynamicRoutes(new RouteBuildEvent($collection, 'dynamic_routes'));
+ $subscriber->onDynamicRoutes(new RouteBuildEvent($collection, 'dynamic_routes'));
// Check the controller defaults.
foreach ($collection as $id => $route) {
diff --git a/core/modules/views/tests/Drupal/views/Tests/EventSubscriber/RouteSubscriberTest.php b/core/modules/views/tests/Drupal/views/Tests/EventSubscriber/RouteSubscriberTest.php
index a6a6d7c..fad9eac 100644
--- a/core/modules/views/tests/Drupal/views/Tests/EventSubscriber/RouteSubscriberTest.php
+++ b/core/modules/views/tests/Drupal/views/Tests/EventSubscriber/RouteSubscriberTest.php
@@ -75,9 +75,9 @@ class RouteSubscriberTest extends UnitTestCase {
}
/**
- * Tests the dynamicRoutes method.
+ * Tests the onDynamicRoutes method.
*
- * @see \Drupal\views\EventSubscriber\RouteSubscriber::dynamicRoutes()
+ * @see \Drupal\views\EventSubscriber\RouteSubscriber::onDynamicRoutes()
*/
public function testDynamicRoutes() {
$collection = new RouteCollection();
@@ -92,7 +92,7 @@ class RouteSubscriberTest extends UnitTestCase {
->method('collectRoutes')
->will($this->returnValue(array('test_id.page_2' => 'views.test_id.page_2')));
- $this->assertNull($this->routeSubscriber->dynamicRoutes($route_event));
+ $this->assertNull($this->routeSubscriber->onDynamicRoutes($route_event));
$this->state->expects($this->once())
->method('set')
@@ -101,11 +101,11 @@ class RouteSubscriberTest extends UnitTestCase {
}
/**
- * Tests the alterRoutes method.
+ * Tests the onAlterRoutes method.
*
- * @see \Drupal\views\EventSubscriber\RouteSubscriber::alterRoutes()
+ * @see \Drupal\views\EventSubscriber\RouteSubscriber::onAlterRoutes()
*/
- public function testAlterRoutes() {
+ public function testOnAlterRoutes() {
$collection = new RouteCollection();
$collection->add('test_route', new Route('test_route', array('_controller' => 'Drupal\Tests\Core\Controller\TestController')));
$route_2 = new Route('test_route/example', array('_controller' => 'Drupal\Tests\Core\Controller\TestController'));
@@ -130,12 +130,12 @@ class RouteSubscriberTest extends UnitTestCase {
->method('collectRoutes')
->will($this->returnValue(array('test_id.page_2' => 'views.test_id.page_2')));
- $this->assertNull($this->routeSubscriber->alterRoutes($route_event));
+ $this->assertNull($this->routeSubscriber->onAlterRoutes($route_event));
// Ensure that after the alterRoutes the collectRoutes method is just called
// once (not for page_1 anymore).
- $this->assertNull($this->routeSubscriber->dynamicRoutes($route_event));
+ $this->assertNull($this->routeSubscriber->onDynamicRoutes($route_event));
$this->state->expects($this->once())
->method('set')
diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php
index aea9434..0018ffd 100644
--- a/core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php
+++ b/core/tests/Drupal/Tests/Core/EventSubscriber/ModuleRouteSubscriberTest.php
@@ -69,7 +69,7 @@ class ModuleRouteSubscriberTest extends UnitTestCase {
$event = new RouteBuildEvent($collection, 'test');
$route_subscriber = new ModuleRouteSubscriber($this->moduleHandler);
- $route_subscriber->removeRoutes($event);
+ $route_subscriber->onAlterRoutes($event);
if ($removed) {
$this->assertNull($collection->get($route_name));
diff --git a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php
index 7555768..f003327 100644
--- a/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php
+++ b/core/tests/Drupal/Tests/Core/EventSubscriber/SpecialAttributesRouteSubscriberTest.php
@@ -84,7 +84,7 @@ class SpecialAttributesRouteSubscriberTest extends UnitTestCase {
}
/**
- * Tests the onRouteBuilding method for valid variables.
+ * Tests the onAlterRoutes method for valid variables.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check.
@@ -95,11 +95,11 @@ class SpecialAttributesRouteSubscriberTest extends UnitTestCase {
$route_collection = new RouteCollection();
$route_collection->add('test', $route);
$event = new RouteBuildEvent($route_collection, 'test');
- $this->assertTrue($this->specialAttributesRouteSubscriber->onRouteBuilding($event));
+ $this->assertTrue($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
}
/**
- * Tests the onRouteBuilding method for invalid variables.
+ * Tests the onAlterRoutes method for invalid variables.
*
* @param \Symfony\Component\Routing\Route $route
* The route to check.
@@ -110,7 +110,7 @@ class SpecialAttributesRouteSubscriberTest extends UnitTestCase {
$route_collection = new RouteCollection();
$route_collection->add('test', $route);
$event = new RouteBuildEvent($route_collection, 'test');
- $this->assertFalse($this->specialAttributesRouteSubscriber->onRouteBuilding($event));
+ $this->assertFalse($this->specialAttributesRouteSubscriber->onAlterRoutes($event));
}
}