diff --git a/core/modules/config_translation/lib/Drupal/config_translation/ConfigMapperInterface.php b/core/modules/config_translation/lib/Drupal/config_translation/ConfigMapperInterface.php index 45214ee..4def427 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/ConfigMapperInterface.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/ConfigMapperInterface.php @@ -52,6 +52,8 @@ public function getBaseRoute(); * * @param \Symfony\Component\Routing\Route $route * The route object used as base route. + * + * @see \Drupal\config_translation\Routing\RouteSubscriber::alterRoutes() */ public function setBaseRoute(Route $route); diff --git a/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php b/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php index 3f6a208..1351e72 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/ConfigNamesMapper.php @@ -144,6 +144,9 @@ public function getBaseRouteParameters() { * {@inheritdoc} */ public function getBaseRoute() { + // The base route cannot be set directly in the constructor because during + // route rebuilding we need to instantiate the mappers before the respective + // routes are available. if (!isset($this->baseRoute)) { $this->baseRoute = $this->routeProvider->getRouteByName($this->getBaseRouteName()); } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Routing/RouteSubscriber.php b/core/modules/config_translation/lib/Drupal/config_translation/Routing/RouteSubscriber.php index 2614dc8..6f24a68 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Routing/RouteSubscriber.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Routing/RouteSubscriber.php @@ -18,11 +18,18 @@ class RouteSubscriber extends RouteSubscriberBase { /** - * The mapper plugin discovery service. + * An array of all configuration mappers, keyed by ID. * - * @var \Drupal\config_translation\ConfigMapperManagerInterface + * @var \Drupal\config_translation\ConfigMapperInterface[] */ - protected $mapperManager; + protected $mappers; + + /** + * A mapping of configuration mappers to their base route name. + * + * @var array + */ + protected $baseRouteNames = array(); /** * Constructs a new RouteSubscriber. @@ -31,13 +38,14 @@ class RouteSubscriber extends RouteSubscriberBase { * The mapper plugin discovery service. */ public function __construct(ConfigMapperManagerInterface $mapper_manager) { - $this->mapperManager = $mapper_manager; + $this->mappers = $mapper_manager->getMappers(); - $this->mappers = $this->mapperManager->getMappers(); - $this->neededMapperBaseRoutes = array(); - $this->baseRoutesMapping = array(); - foreach ($this->mappers as $id => $mapper) { - $this->neededMapperBaseRoutes[$id] = $mapper->getBaseRouteName(); + // The entire list of base route names is needed so we can check each route + // collection for matching routes in RouteSubscriber::alterRoutes(). In + // order to pass the route information to the configuration mapper we then + // need the respective mapper ID for the base route name. + foreach ($this->mappers as $mapper_id => $mapper) { + $this->baseRouteNames[$mapper_id] = $mapper->getBaseRouteName(); } } @@ -45,21 +53,22 @@ public function __construct(ConfigMapperManagerInterface $mapper_manager) { * {@inheritdoc} */ protected function alterRoutes(RouteCollection $collection, $provider) { - foreach ($this->neededMapperBaseRoutes as $mapper_id => $route_name) { + foreach ($this->baseRouteNames as $mapper_id => $route_name) { if ($route = $collection->get($route_name)) { - $this->baseRoutesMapping[$mapper_id] = $route; - } - } + $mapper = $this->mappers[$mapper_id]; + $mapper->setBaseRoute($route); + $collection->add($mapper->getOverviewRouteName(), $mapper->getOverviewRoute()); + $collection->add($mapper->getAddRouteName(), $mapper->getAddRoute()); + $collection->add($mapper->getEditRouteName(), $mapper->getEditRoute()); + $collection->add($mapper->getDeleteRouteName(), $mapper->getDeleteRoute()); - foreach ($this->mappers as $id => $mapper) { - if (!isset($this->baseRoutesMapping[$id])) { - throw new \Exception('No freaking idea: '); + // We do not need to check for the same base route name on the next + // invocation. + unset($this->baseRouteNames[$mapper_id]); } - $mapper->setBaseRoute($this->baseRoutesMapping[$id]); - $collection->add($mapper->getOverviewRouteName(), $mapper->getOverviewRoute()); - $collection->add($mapper->getAddRouteName(), $mapper->getAddRoute()); - $collection->add($mapper->getEditRouteName(), $mapper->getEditRoute()); - $collection->add($mapper->getDeleteRouteName(), $mapper->getDeleteRoute()); + } + if (!empty($this->baseRouteNames)) { + throw new \Exception('Well, if @dawehner has NFI, what is @tstoeckler supposed to say?!'); } } @@ -68,8 +77,9 @@ protected function alterRoutes(RouteCollection $collection, $provider) { */ public static function getSubscribedEvents() { $events = parent::getSubscribedEvents(); + // This route subscriber adds routes based on the existence of other routes, + // so all route subscribers that add routes need to run before this one. $events[RoutingEvents::ALTER] = array('onAlterRoutes', -10); - return $events; }