diff --git a/src/Routing/PageManagerRoutes.php b/src/Routing/PageManagerRoutes.php index 103137f..b9c1680 100644 --- a/src/Routing/PageManagerRoutes.php +++ b/src/Routing/PageManagerRoutes.php @@ -56,9 +56,8 @@ protected function alterRoutes(RouteCollection $collection) { /** @var $entity \Drupal\page_manager\PageInterface */ // If the page is disabled skip making a route for it. - if (!$entity->status() || $entity->isFallbackPage()) { - continue; - } + // @todo The fallback page logic seems backward. + $modify_collection = $entity->status() && !$entity->isFallbackPage(); // Prepare the values that need to be altered for an existing page. $parameters = [ @@ -74,30 +73,34 @@ protected function alterRoutes(RouteCollection $collection) { $path = $collection_route->getPath(); $parameters += $collection_route->getOption('parameters') ?: []; - $collection->remove($route_name); + if ($modify_collection) { + $collection->remove($route_name); + } } else { $route_name = "page_manager.page_view_$entity_id"; $path = $entity->getPath(); } - // Construct and add a new route. - $route = new Route( - $path, - [ - '_entity_view' => 'page_manager_page', - 'page_manager_page' => $entity_id, - '_title' => $entity->label(), - ], - [ - '_entity_access' => 'page_manager_page.view', - ], - [ - 'parameters' => $parameters, - '_admin_route' => $entity->usesAdminTheme(), - ] - ); - $collection->add($route_name, $route); + if ($modify_collection) { + // Construct and add a new route. + $route = new Route( + $path, + [ + '_entity_view' => 'page_manager_page', + 'page_manager_page' => $entity_id, + '_title' => $entity->label(), + ], + [ + '_entity_access' => 'page_manager_page.view', + ], + [ + 'parameters' => $parameters, + '_admin_route' => $entity->usesAdminTheme(), + ] + ); + $collection->add($route_name, $route); + } } } diff --git a/tests/src/Unit/PageManagerRoutesTest.php b/tests/src/Unit/PageManagerRoutesTest.php index 6df2457..3fe818a 100644 --- a/tests/src/Unit/PageManagerRoutesTest.php +++ b/tests/src/Unit/PageManagerRoutesTest.php @@ -230,6 +230,41 @@ public function providerTestAlterRoutesOverrideExisting() { } /** + * @covers ::alterRoutes + */ + public function testAlterRoutesOverrideExistingDisabled() { + $page_path = '/page'; + $existing_route_path = '/page'; + $route_name = 'test_route'; + // Set up a page with the same path as an existing route. + $page = $this->prophesize(PageInterface::class); + $page->status()->willReturn(FALSE); + $page->getPath()->willReturn($page_path); + $page->isFallbackPage()->willReturn(FALSE); + $page->label()->willReturn(NULL); + $page->usesAdminTheme()->willReturn(FALSE); + + $this->pageStorage->loadMultiple() + ->willReturn(['page1' => $page->reveal()]) + ->shouldBeCalledTimes(1); + + $this->cacheTagsInvalidator->invalidateTags(["page_manager_route_name:$route_name"])->shouldBeCalledTimes(1); + + $collection = new RouteCollection(); + $existing_route = new Route($existing_route_path, [], [], ['parameters' => ['foo' => 'bar']]); + $collection->add($route_name, $existing_route); + $route_event = new RouteBuildEvent($collection); + $this->routeSubscriber->onAlterRoutes($route_event); + + // The normal route name is not used, the existing route name is instead. + $this->assertSame(1, $collection->count()); + $this->assertNull($collection->get('page_manager.page_view_page1')); + + $route = $collection->get($route_name); + $this->assertSame($existing_route, $route); + } + + /** * Asserts that a route object has the expected properties. * * @param \Symfony\Component\Routing\Route $route