diff --git a/src/Routing/PageManagerRoutes.php b/src/Routing/PageManagerRoutes.php index eb68f65..039e83e 100644 --- a/src/Routing/PageManagerRoutes.php +++ b/src/Routing/PageManagerRoutes.php @@ -16,6 +16,8 @@ use Symfony\Component\Routing\RouteCollection; /** * Provides routes for page entities. + * + * @see \Drupal\page_manager\Entity\PageViewBuilder */ class PageManagerRoutes extends RouteSubscriberBase { @@ -53,12 +55,14 @@ class PageManagerRoutes extends RouteSubscriberBase { // Prepare the values that need to be altered for an existing page. $path = $entity->getPath(); + $parameters = array( 'page_manager_page' => array( 'type' => 'entity:page', ), ); + $altered_route = FALSE; // Loop through all existing routes to see if this is overriding a route. foreach ($collection->all() as $name => $collection_route) { // Find all paths which match the path of the current display. @@ -72,6 +76,8 @@ class PageManagerRoutes extends RouteSubscriberBase { // Merge in any route parameter definitions. $parameters += $collection_route->getOption('parameters') ?: array(); + $altered_route = TRUE; + // Update the route name this will be added to. $route_name = $name; // Remove the existing route. @@ -80,13 +86,28 @@ class PageManagerRoutes extends RouteSubscriberBase { } } + $arguments = []; + // Replace % with proper route slugs ("{arg_$i}"). + if (!$altered_route) { + $bits = explode('/', $path); + $arg_counter = 0; + foreach ($bits as $pos => $bit) { + if ($bit == '%') { + $arg_id = 'arg_' . $arg_counter++; + $arguments[$arg_id] = NULL; + $bits[$pos] = '{' . $arg_id . '}'; + } + } + $path = implode("/", $bits); + } + // Construct an add a new route. $route = new Route( $path, array( '_entity_view' => 'page_manager_page', 'page_manager_page' => $entity_id, - ), + ) + $arguments, array( '_entity_access' => 'page_manager_page.view', ), diff --git a/tests/src/Unit/PageManagerRoutesTest.php b/tests/src/Unit/PageManagerRoutesTest.php index 6c9b2cf..36fcaf3 100644 --- a/tests/src/Unit/PageManagerRoutesTest.php +++ b/tests/src/Unit/PageManagerRoutesTest.php @@ -157,6 +157,54 @@ class PageManagerRoutesTest extends UnitTestCase { } /** + * Tests adding routes for an argument. + * + * @covers ::alterRoutes + */ + public function testAlterRoutesWithArgument() { + // Set up a valid page. + $page1 = $this->getMock('Drupal\page_manager\PageInterface'); + $page1->expects($this->once()) + ->method('status') + ->willReturn(True); + $page1->expects($this->once()) + ->method('getPath') + ->willReturn('/page1/%'); + $page1->expects($this->once()) + ->method('usesAdminTheme') + ->willReturn(True); + $pages['page1'] = $page1; + + $this->pageStorage->expects($this->once()) + ->method('loadMultiple') + ->will($this->returnValue($pages)); + + $collection = new RouteCollection(); + $route_event = new RouteBuildEvent($collection); + $this->routeSubscriber->onAlterRoutes($route_event); + + $this->assertSame(1, $collection->count()); + $route = $collection->get('page_manager.page_view_page1'); + $expected_defaults = array( + '_entity_view' => 'page_manager_page', + 'page_manager_page' => 'page1', + ); + $expected_requirements = array( + '_entity_access' => 'page_manager_page.view', + ); + $expected_options = array( + 'compiler_class' => 'Symfony\Component\Routing\RouteCompiler', + 'parameters' => array( + 'page_manager_page' => array( + 'type' => 'entity:page', + ), + ), + '_admin_route' => TRUE, + ); + $this->assertMatchingRoute($route, '/page1/{arg_0}', $expected_defaults, $expected_requirements, $expected_options); + } + + /** * Tests overriding an existing route. * * @covers ::alterRoutes