diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index ec5ee33..b0a7f0a 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -308,7 +308,7 @@ public function getRouteProviders($entity_type) { $route_provider_classes = $this->getDefinition($entity_type, TRUE)->getRouteProviderClasses(); foreach ($route_provider_classes as $type => $class) { - $this->handlers['route_provider'][$entity_type][$type] = $this->doInstantiateHandler($class, $this->getDefinition($entity_type)); + $this->handlers['route_provider'][$entity_type][$type] = $this->instantiateHandler($class, $this->getDefinition($entity_type)); } } return isset($this->handlers['route_provider'][$entity_type]) ? $this->handlers['route_provider'][$entity_type] : []; @@ -338,7 +338,7 @@ public function getHandler($entity_type, $handler_type) { if (!$class) { throw new InvalidPluginDefinitionException($entity_type, sprintf('The "%s" entity type did not specify a %s handler.', $entity_type, $handler_type)); } - $this->handlers[$handler_type][$entity_type] = $this->doInstantiateHandler($class, $definition); + $this->handlers[$handler_type][$entity_type] = $this->instantiateHandler($class, $definition); } return $this->handlers[$handler_type][$entity_type]; } @@ -1234,16 +1234,17 @@ protected function deleteLastInstalledFieldStorageDefinition(FieldStorageDefinit } /** - * Instantiation the handler. + * Create an instance of a handler from the class of the handler. * * @param string $class * The class to instantiation. * @param \Drupal\Core\Entity\EntityTypeInterface $definition * The entity type definition. * - * @return mixed + * @return object + * The instantiated handler object. */ - protected function doInstantiateHandler($class, EntityTypeInterface $definition) { + protected function instantiateHandler($class, EntityTypeInterface $definition) { if (is_subclass_of($class, 'Drupal\Core\Entity\EntityHandlerInterface')) { $handler = $class::createInstance($this->container, $definition); } diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php index 706f6bb..d6640b1 100644 --- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php @@ -284,7 +284,7 @@ public function hasFormClasses(); * * Much like forms you can define multiple route provider handlers. * - * @return array + * @return string[] */ public function getRouteProviderClasses(); diff --git a/core/lib/Drupal/Core/Entity/Routing/EntityRouteProviderInterface.php b/core/lib/Drupal/Core/Entity/Routing/EntityRouteProviderInterface.php index b46ed72..1f99cc7 100644 --- a/core/lib/Drupal/Core/Entity/Routing/EntityRouteProviderInterface.php +++ b/core/lib/Drupal/Core/Entity/Routing/EntityRouteProviderInterface.php @@ -18,11 +18,13 @@ /** * Provides routes for entities. * - * @param \Symfony\Component\Routing\RouteCollection $route_collection - * The route collection. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type * The entity type + * + * @return \Symfony\Component\Routing\RouteCollection|\Symfony\Component\Routing\Route[] + * Returns a route collection or an array of routes keyed by name, like + * route_callbacks inside 'routing.yml' files. */ - public function provideRoutes(RouteCollection $route_collection, EntityTypeInterface $entity_type); + public function routes(EntityTypeInterface $entity_type); } diff --git a/core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php index 7c5bfbd..98a6a49 100644 --- a/core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/EntityRouteProviderSubscriber.php @@ -11,6 +11,7 @@ use Drupal\Core\Routing\RouteBuildEvent; use Drupal\Core\Routing\RoutingEvents; use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\Routing\RouteCollection; /** * Ensures that routes can be provided by entity types. @@ -38,9 +39,22 @@ public function __construct(EntityManagerInterface $entity_manager) { * Provide routes on route rebuild time. */ public function onDynamic(RouteBuildEvent $event) { + $route_collection = $event->getRouteCollection(); foreach ($this->entityManager->getDefinitions() as $entity_type) { - foreach ($this->entityManager->getRouteProviders($entity_type->id()) as $route_provider) { - $route_provider->provideRoutes($event->getRouteCollection(), $entity_type); + if ($entity_type->hasHandlerClass('route_provider', TRUE)) { + foreach ($this->entityManager->getRouteProviders($entity_type->id()) as $route_provider) { + // Allow to both return an array of routes or a route collection, + // like route_callbacks in the routing.yml file. + $routes = $route_provider->routes($entity_type); + if ($routes instanceof RouteCollection) { + $route_collection->addCollection($routes); + } + elseif (is_array($routes)) { + foreach ($routes as $route_name => $route) { + $route_collection->add($route_name, $route); + } + } + } } } } diff --git a/core/modules/node/src/Entity/NodeRouteProvider.php b/core/modules/node/src/Entity/NodeRouteProvider.php index f84b729..7041900 100644 --- a/core/modules/node/src/Entity/NodeRouteProvider.php +++ b/core/modules/node/src/Entity/NodeRouteProvider.php @@ -20,7 +20,8 @@ class NodeRouteProvider implements EntityRouteProviderInterface { /** * {@inheritdoc} */ - public function provideRoutes(RouteCollection $route_collection, EntityTypeInterface $entity_type) { + public function routes( EntityTypeInterface $entity_type) { + $route_collection = new RouteCollection(); $route = (new Route('/node/{node}')) ->addDefaults([ '_content' => '\Drupal\node\Controller\NodeViewController::view', @@ -43,6 +44,8 @@ public function provideRoutes(RouteCollection $route_collection, EntityTypeInter ->setRequirement('_entity_access', 'node.update') ->setOption('_node_operation_route', TRUE); $route_collection->add('entity.node.edit_form', $route); + + return $route_collection; } } diff --git a/core/modules/user/src/Entity/UserRouteProvider.php b/core/modules/user/src/Entity/UserRouteProvider.php index a18916a..abeaeb1 100644 --- a/core/modules/user/src/Entity/UserRouteProvider.php +++ b/core/modules/user/src/Entity/UserRouteProvider.php @@ -20,7 +20,8 @@ class UserRouteProvider implements EntityRouteProviderInterface { /** * {@inheritdoc} */ - public function provideRoutes(RouteCollection $route_collection, EntityTypeInterface $entity_type) { + public function routes(EntityTypeInterface $entity_type) { + $route_collection = new RouteCollection(); $route = (new Route('/user/{user}')) ->setDefaults([ '_entity_view' => 'user.full', @@ -46,6 +47,8 @@ public function provideRoutes(RouteCollection $route_collection, EntityTypeInter ->setOption('_admin_route', TRUE) ->setRequirement('_entity_access', 'user.delete'); $route_collection->add('entity.user.cancel_form', $route); + + return $route_collection; } } diff --git a/core/modules/user/user.routing.yml b/core/modules/user/user.routing.yml index 94ee58c..789cd83 100644 --- a/core/modules/user/user.routing.yml +++ b/core/modules/user/user.routing.yml @@ -143,26 +143,6 @@ user.login: options: _maintenance_access: TRUE -entity.user.edit_form: - path: '/user/{user}/edit' - defaults: - _entity_form: 'user.default' - _title_callback: 'Drupal\user\Controller\UserController::userTitle' - options: - _admin_route: TRUE - requirements: - _entity_access: 'user.update' - -entity.user.cancel_form: - path: '/user/{user}/cancel' - defaults: - _title: 'Cancel account' - _entity_form: 'user.cancel' - options: - _admin_route: TRUE - requirements: - _entity_access: 'user.delete' - user.cancel_confirm: path: '/user/{user}/cancel/confirm/{timestamp}/{hashed_pass}' defaults: