diff --git a/core/modules/views/src/EventSubscriber/RouteSubscriber.php b/core/modules/views/src/EventSubscriber/RouteSubscriber.php index abc5bb6..82c7897 100644 --- a/core/modules/views/src/EventSubscriber/RouteSubscriber.php +++ b/core/modules/views/src/EventSubscriber/RouteSubscriber.php @@ -150,11 +150,10 @@ protected function alterRoutes(RouteCollection $collection) { // If the display returns TRUE a route item was found, so it does not // have to be added. $view_route_names = $display->alterRoutes($collection); - $this->viewRouteNames = $view_route_names + $this->viewRouteNames; - foreach ($view_route_names as $id_display => $route_name) { - $view_route_name = $this->viewsDisplayPairs[$id_display]; - unset($this->viewsDisplayPairs[$id_display]); - $collection->remove("views.$view_route_name"); + if (!empty($view_route_names)) { + // Update the class properties with the new route names. + $this->viewRouteNames = $view_route_names + $this->viewRouteNames; + $this->viewsDisplayPairs = array_diff_key($this->viewsDisplayPairs, $view_route_names); } } } diff --git a/core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php b/core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php index 434a503..8f9b1a5 100644 --- a/core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php +++ b/core/modules/views/src/Plugin/Derivative/ViewsLocalTask.php @@ -11,6 +11,7 @@ use Drupal\Component\Plugin\Derivative\DeriverBase; use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface; use Drupal\Core\Routing\RouteProviderInterface; +use Drupal\views\Plugin\views\display\DisplayRouterInterface; use Drupal\views\Views; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -70,7 +71,7 @@ public function getDerivativeDefinitions($base_plugin_definition) { $executable->setDisplay($display_id); $menu = $executable->display_handler->getOption('menu'); if (in_array($menu['type'], array('tab', 'default tab'))) { - $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id; + $plugin_id = DisplayRouterInterface::PREFIX . $executable->storage->id() . '.' . $display_id; $route_name = $view_route_names[$executable->storage->id() . '.' . $display_id]; // Don't add a local task for views which override existing routes. @@ -109,7 +110,7 @@ public function alterLocalTasks(&$local_tasks) { // We already have set the base_route for default tabs. if (in_array($menu['type'], array('tab'))) { - $plugin_id = 'view.' . $executable->storage->id() . '.' . $display_id; + $plugin_id = DisplayRouterInterface::PREFIX . $executable->storage->id() . '.' . $display_id; $view_route_name = $view_route_names[$executable->storage->id() . '.' . $display_id]; // Don't add a local task for views which override existing routes. diff --git a/core/modules/views/src/Plugin/views/display/DisplayRouterInterface.php b/core/modules/views/src/Plugin/views/display/DisplayRouterInterface.php index 65328ed..1814730 100644 --- a/core/modules/views/src/Plugin/views/display/DisplayRouterInterface.php +++ b/core/modules/views/src/Plugin/views/display/DisplayRouterInterface.php @@ -18,6 +18,11 @@ interface DisplayRouterInterface extends DisplayPluginInterface { /** + * Prefix added to the beginning of a view entity's dynamic routes. + */ + const PREFIX = 'views.view.'; + + /** * Adds the route entry of a view to the collection. * * @param \Symfony\Component\Routing\RouteCollection $collection diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index 415b704..c2514a7 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -217,7 +217,7 @@ public function collectRoutes(RouteCollection $collection) { $route = $this->getRoute($view_id, $display_id); if (!($route_name = $this->getOption('route_name'))) { - $route_name = "view.$view_id.$display_id"; + $route_name = static::PREFIX . "$view_id.$display_id"; } $collection->add($route_name, $route); return array("$view_id.$display_id" => $route_name); @@ -502,7 +502,7 @@ public function getRouteName() { // Check for overridden route names. $view_route_names = $this->getAlteredRouteNames(); - return (isset($view_route_names[$view_route_key]) ? $view_route_names[$view_route_key] : "view.$view_route_key"); + return (isset($view_route_names[$view_route_key]) ? $view_route_names[$view_route_key] : static::PREFIX . $view_route_key); } /** diff --git a/core/modules/views/src/Tests/Wizard/BasicTest.php b/core/modules/views/src/Tests/Wizard/BasicTest.php index 5871afc..ed5397f 100644 --- a/core/modules/views/src/Tests/Wizard/BasicTest.php +++ b/core/modules/views/src/Tests/Wizard/BasicTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Serialization\Json; use Drupal\Component\Utility\String; use Drupal\Core\Url; +use Drupal\views\Plugin\views\display\DisplayRouterInterface; use Drupal\views\Views; /** @@ -75,8 +76,8 @@ function testViewsWizardAndListing() { $this->assertText($node2->label()); // Check if we have the feed. - $this->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.feed_1')->toString()); - $elements = $this->cssSelect('link[href="' . Url::fromRoute('view.' . $view2['id'] . '.feed_1', [], ['absolute' => TRUE])->toString() . '"]'); + $this->assertLinkByHref(Url::fromRoute(DisplayRouterInterface::PREFIX . $view2['id'] . '.feed_1')->toString()); + $elements = $this->cssSelect('link[href="' . Url::fromRoute(DisplayRouterInterface::PREFIX . $view2['id'] . '.feed_1', [], ['absolute' => TRUE])->toString() . '"]'); $this->assertEqual(count($elements), 1, 'Feed found.'); $this->drupalGet($view2['page[feed_properties][path]']); $this->assertRaw('drupalGet('admin/structure/views'); $this->assertText($view2['label']); $this->assertText($view2['description']); - $this->assertLinkByHref(Url::fromRoute('view.' . $view2['id'] . '.page_1')->toString()); + $this->assertLinkByHref(Url::fromRoute(DisplayRouterInterface::PREFIX . $view2['id'] . '.page_1')->toString()); // The view should not have a REST export display. $this->assertNoText('REST export', 'If only the page option was enabled in the wizard, the resulting view does not have a REST export display.'); @@ -126,7 +127,7 @@ function testViewsWizardAndListing() { $this->drupalGet('admin/structure/views'); $this->assertText($view3['label']); $this->assertText($view3['description']); - $this->assertLinkByHref(Url::fromRoute('view.' . $view3['id'] . '.page_1')->toString()); + $this->assertLinkByHref(Url::fromRoute(DisplayRouterInterface::PREFIX . $view3['id'] . '.page_1')->toString()); // The view should not have a REST export display. $this->assertNoText('REST export', 'If only the page and block options were enabled in the wizard, the resulting view does not have a REST export display.'); diff --git a/core/modules/views/src/Tests/Wizard/MenuTest.php b/core/modules/views/src/Tests/Wizard/MenuTest.php index 6f29329..d6af3ab 100644 --- a/core/modules/views/src/Tests/Wizard/MenuTest.php +++ b/core/modules/views/src/Tests/Wizard/MenuTest.php @@ -49,7 +49,7 @@ function testMenus() { /** @var \Drupal\Core\Menu\MenuLinkInterface $link */ $link = $menu_link_manager->createInstance('views_view:views.' . $view['id'] . '.page_1'); $url = $link->getUrlObject(); - $this->assertEqual($url->getRouteName(), 'view.' . $view['id'] . '.page_1', String::format('Found a link to %path in the main menu', array('%path' => $view['page[path]']))); + $this->assertEqual($url->getRouteName(), DisplayRouterInterface::PREFIX . $view['id'] . '.page_1', String::format('Found a link to %path in the main menu', array('%path' => $view['page[path]']))); $metadata = $link->getMetaData(); $this->assertEqual(array('view_id' => $view['id'], 'display_id' => 'page_1'), $metadata); } diff --git a/core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php b/core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php index 9be3036..da43665 100644 --- a/core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php +++ b/core/modules/views/tests/src/Unit/EventSubscriber/RouteSubscriberTest.php @@ -90,8 +90,7 @@ public function testOnAlterRoutes() { $collection = new RouteCollection(); // The first route will be overridden later. $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')); - $collection->add('test_route_2', $route_2); + $collection->add('test_route_2', new Route('test_route/example', array('_controller' => 'Drupal\Tests\Core\Controller\TestController'))); $route_event = new RouteBuildEvent($collection, 'views'); @@ -102,18 +101,21 @@ public function testOnAlterRoutes() { $display_1->expects($this->once()) ->method('collectRoutes') ->willReturnCallback(function() use ($collection) { - $collection->add('views.test_id.page_1', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController'])); - return ['test_id.page_1' => 'views.test_id.page_1']; + $collection->add('views.view.test_id.page_1', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController'])); + return ['test_id.page_1' => 'views.view.test_id.page_1']; }); $display_1->expects($this->once()) ->method('alterRoutes') - ->willReturn(['test_id.page_1' => 'test_route']); + ->willReturnCallback(function() use ($collection) { + $collection->remove('views.view.test_id.page_1'); + return ['test_id.page_1' => 'test_route']; + }); $display_2->expects($this->once()) ->method('collectRoutes') ->willReturnCallback(function() use ($collection) { - $collection->add('views.test_id.page_2', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController'])); - return ['test_id.page_2' => 'views.test_id.page_2']; + $collection->add('views.view.test_id.page_2', new Route('test_route', ['_controller' => 'Drupal\views\Routing\ViewPageController'])); + return ['test_id.page_2' => 'views.view.test_id.page_2']; }); $display_2->expects($this->once()) ->method('alterRoutes') @@ -128,10 +130,10 @@ public function testOnAlterRoutes() { $this->state->expects($this->once()) ->method('set') - ->with('views.view_route_names', array('test_id.page_1' => 'test_route', 'test_id.page_2' => 'views.test_id.page_2')); + ->with('views.view_route_names', array('test_id.page_1' => 'test_route', 'test_id.page_2' => 'views.view.test_id.page_2')); $collection = $route_event->getRouteCollection(); - $this->assertEquals(['test_route', 'test_route_2', 'views.test_id.page_2'], array_keys($collection->all())); + $this->assertEquals(['test_route', 'test_route_2', 'views.view.test_id.page_2'], array_keys($collection->all())); $this->routeSubscriber->routeRebuildFinished(); } diff --git a/core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php b/core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php index c75d96f..bf4128f 100644 --- a/core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/Derivative/ViewsLocalTaskTest.php @@ -9,6 +9,7 @@ use Drupal\Tests\UnitTestCase; use Drupal\views\Plugin\Derivative\ViewsLocalTask; +use Drupal\views\Plugin\views\display\DisplayRouterInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -118,7 +119,8 @@ public function testGetDerivativeDefinitionsWithLocalTask() { // Mock the view route names state. $view_route_names = array(); - $view_route_names['example_view.page_1'] = 'view.example_view.page_1'; + $route_name = DisplayRouterInterface::PREFIX . 'example_view.page_1'; + $view_route_names['example_view.page_1'] = $route_name; $this->state->expects($this->once()) ->method('get') ->with('views.view_route_names') @@ -126,11 +128,12 @@ public function testGetDerivativeDefinitionsWithLocalTask() { $definitions = $this->localTaskDerivative->getDerivativeDefinitions($this->baseDefinition); $this->assertCount(1, $definitions); - $this->assertEquals('view.example_view.page_1', $definitions['view.example_view.page_1']['route_name']); - $this->assertEquals(12, $definitions['view.example_view.page_1']['weight']); - $this->assertEquals('Example title', $definitions['view.example_view.page_1']['title']); - $this->assertEquals($this->baseDefinition['class'], $definitions['view.example_view.page_1']['class']); - $this->assertTrue(empty($definitions['view.example_view.page_1']['base_route'])); + $definition = $definitions[$route_name]; + $this->assertEquals($route_name, $definition['route_name']); + $this->assertEquals(12, $definition['weight']); + $this->assertEquals('Example title', $definition['title']); + $this->assertEquals($this->baseDefinition['class'], $definition['class']); + $this->assertTrue(empty($definition['base_route'])); } /** @@ -204,7 +207,8 @@ public function testGetDerivativeDefinitionsWithDefaultLocalTask() { // Mock the view route names state. $view_route_names = array(); - $view_route_names['example_view.page_1'] = 'view.example_view.page_1'; + $route_name = DisplayRouterInterface::PREFIX .'example_view.page_1'; + $view_route_names['example_view.page_1'] = $route_name; $this->state->expects($this->exactly(2)) ->method('get') ->with('views.view_route_names') @@ -212,25 +216,26 @@ public function testGetDerivativeDefinitionsWithDefaultLocalTask() { $definitions = $this->localTaskDerivative->getDerivativeDefinitions($this->baseDefinition); $this->assertCount(1, $definitions); - $plugin = $definitions['view.example_view.page_1']; - $this->assertEquals('view.example_view.page_1', $plugin['route_name']); + $plugin = $definitions[$route_name]; + $this->assertEquals($route_name, $plugin['route_name']); $this->assertEquals(12, $plugin['weight']); $this->assertEquals('Example title', $plugin['title']); $this->assertEquals($this->baseDefinition['class'], $plugin['class']); - $this->assertEquals('view.example_view.page_1', $plugin['base_route']); + $this->assertEquals($route_name, $plugin['base_route']); // Setup the prefix of the derivative. - $definitions['views_view:view.example_view.page_1'] = $definitions['view.example_view.page_1']; - unset($definitions['view.example_view.page_1']); + $route_name = DisplayRouterInterface::PREFIX . 'example_view.page_1'; + $definitions['views_view:' . $route_name] = $definitions[$route_name]; + unset($definitions[$route_name]); $this->localTaskDerivative->alterLocalTasks($definitions); - $plugin = $definitions['views_view:view.example_view.page_1']; + $plugin = $definitions['views_view:' . $route_name]; $this->assertCount(1, $definitions); - $this->assertEquals('view.example_view.page_1', $plugin['route_name']); + $this->assertEquals($route_name, $plugin['route_name']); $this->assertEquals(12, $plugin['weight']); $this->assertEquals('Example title', $plugin['title']); $this->assertEquals($this->baseDefinition['class'], $plugin['class']); - $this->assertEquals('view.example_view.page_1', $plugin['base_route']); + $this->assertEquals($route_name, $plugin['base_route']); } /** @@ -268,7 +273,7 @@ public function testGetDerivativeDefinitionsWithExistingLocalTask() { // Mock the view route names state. $view_route_names = array(); - $view_route_names['example_view.page_1'] = 'view.example_view.page_1'; + $view_route_names['example_view.page_1'] = DisplayRouterInterface::PREFIX . 'example_view.page_1'; $this->state->expects($this->exactly(2)) ->method('get') ->with('views.view_route_names') @@ -292,17 +297,18 @@ public function testGetDerivativeDefinitionsWithExistingLocalTask() { $definitions += $this->localTaskDerivative->getDerivativeDefinitions($this->baseDefinition); // Setup the prefix of the derivative. - $definitions['views_view:view.example_view.page_1'] = $definitions['view.example_view.page_1']; - unset($definitions['view.example_view.page_1']); + $route_name = DisplayRouterInterface::PREFIX . 'example_view.page_1'; + $definitions['views_view:' . $route_name] = $definitions[$route_name]; + unset($definitions[$route_name]); $this->localTaskDerivative->alterLocalTasks($definitions); - $plugin = $definitions['views_view:view.example_view.page_1']; + $plugin = $definitions['views_view:' . $route_name]; $this->assertCount(2, $definitions); // Ensure the other local task was not changed. $this->assertEquals($other_tab, $definitions['test_route_tab']); - $this->assertEquals('view.example_view.page_1', $plugin['route_name']); + $this->assertEquals($route_name, $plugin['route_name']); $this->assertEquals(12, $plugin['weight']); $this->assertEquals('Example title', $plugin['title']); $this->assertEquals($this->baseDefinition['class'], $plugin['class']); diff --git a/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php b/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php index 0cc643a..1824106 100644 --- a/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php +++ b/core/modules/views/tests/src/Unit/Plugin/display/PathPluginBaseTest.php @@ -9,6 +9,7 @@ use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Tests\UnitTestCase; +use Drupal\views\Plugin\views\display\DisplayRouterInterface; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; @@ -101,9 +102,9 @@ public function testCollectRoutes() { $collection = new RouteCollection(); $result = $this->pathPlugin->collectRoutes($collection); - $this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result); + $this->assertEquals(array('test_id.page_1' => DisplayRouterInterface::PREFIX . 'test_id.page_1'), $result); - $route = $collection->get('view.test_id.page_1'); + $route = $collection->get(DisplayRouterInterface::PREFIX . 'test_id.page_1'); $this->assertTrue($route instanceof Route); $this->assertEquals('test_id', $route->getDefault('view_id')); $this->assertEquals('page_1', $route->getDefault('display_id')); @@ -194,9 +195,9 @@ public function testCollectRoutesWithNamedParameters() { $collection = new RouteCollection(); $result = $this->pathPlugin->collectRoutes($collection); - $this->assertEquals(array('test_id.page_1' => 'view.test_id.page_1'), $result); + $this->assertEquals(array('test_id.page_1' => DisplayRouterInterface::PREFIX . 'test_id.page_1'), $result); - $route = $collection->get('view.test_id.page_1'); + $route = $collection->get(DisplayRouterInterface::PREFIX . 'test_id.page_1'); $this->assertTrue($route instanceof Route); $this->assertEquals('/test_route/{node}/example', $route->getPath()); $this->assertEquals('test_id', $route->getDefault('view_id')); @@ -326,7 +327,7 @@ public function testGetRouteName() { $this->pathPlugin->initDisplay($view, $display); $route_name = $this->pathPlugin->getRouteName(); // Ensure that the expected routename is returned. - $this->assertEquals('view.test_id.page_1', $route_name); + $this->assertEquals(DisplayRouterInterface::PREFIX . 'test_id.page_1', $route_name); } /**