diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index 58a9d0f..1d13d55 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -16,6 +16,7 @@ use Drupal\node\Entity\Node; use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Vocabulary; +use Drupal\taxonomy\TermInterface; use Drupal\taxonomy\VocabularyInterface; use Drupal\Component\Utility\String; @@ -130,8 +131,7 @@ function taxonomy_term_uri($term) { */ function taxonomy_page_build(&$page) { $route_match = \Drupal::routeMatch(); - if ($route_match->getRouteName() == 'taxonomy.term_page') { - $term = $route_match->getParameter('taxonomy_term'); + if ($route_match->getRouteName() == 'entity.taxonomy_term.canonical' && ($term = $route_match->getParameter('taxonomy_term')) && $term instanceof TermInterface) { foreach ($term->uriRelationships() as $rel) { // Set the term path as the canonical URL to prevent duplicate content. $page['#attached']['drupal_add_html_head_link'][] = array( diff --git a/core/modules/views/src/EventSubscriber/RouteSubscriber.php b/core/modules/views/src/EventSubscriber/RouteSubscriber.php index 9fec99b..4b92282 100644 --- a/core/modules/views/src/EventSubscriber/RouteSubscriber.php +++ b/core/modules/views/src/EventSubscriber/RouteSubscriber.php @@ -87,6 +87,8 @@ public static function getSubscribedEvents() { $events = parent::getSubscribedEvents(); $events[KernelEvents::VIEW][] = array('onHtmlPage', 75); $events[RoutingEvents::FINISHED] = array('routeRebuildFinished'); + $events[RoutingEvents::ALTER] = ['onAlterRoutes', -175]; + return $events; } diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index aa71acd..168338e 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -239,6 +239,7 @@ public function alterRoutes(RouteCollection $collection) { // requirements). // Replace the existing route with a new one based on views. + $original_route = $collection->get($name); $collection->remove($name); $view_id = $this->view->storage->id(); @@ -254,6 +255,10 @@ public function alterRoutes(RouteCollection $collection) { $path = str_replace('{arg_' . $position . '}', '{' . $parameter_name . '}', $path); $argument_map['arg_' . $position] = $parameter_name; } + // Copy the original options from the route, so for example we ensure + // that parameter conversion options is carried over. + $route->setOptions($route->getOptions() + $original_route->getOptions()); + // Set the corrected path and the mapping to the route object. $route->setOption('_view_argument_map', $argument_map); $route->setPath($path); diff --git a/core/modules/views/tests/src/Plugin/display/PathPluginBaseTest.php b/core/modules/views/tests/src/Plugin/display/PathPluginBaseTest.php index af2365e..b4416e7 100644 --- a/core/modules/views/tests/src/Plugin/display/PathPluginBaseTest.php +++ b/core/modules/views/tests/src/Plugin/display/PathPluginBaseTest.php @@ -241,6 +241,43 @@ public function testAlterRoutesWithParameters() { } /** + * Tests altering routes with parameters and upcasting information + */ + public function testAlterRoutesWithParametersAndUpcasting() { + $collection = new RouteCollection(); + $collection->add('test_route', new Route('test_route/{parameter}', ['_controller' => 'Drupal\Tests\Core\Controller\TestController::content'], [], ['parameters' => ['taxonomy_term' => 'entity:entity_test']])); + + list($view) = $this->setupViewExecutableAccessPlugin(); + + // Manually set up an argument handler. + $argument = $this->getMockBuilder('Drupal\views\Plugin\views\argument\ArgumentPluginBase') + ->disableOriginalConstructor() + ->getMock(); + $view->argument['test_id'] = $argument; + + $display = array(); + $display['display_plugin'] = 'page'; + $display['id'] = 'page_1'; + $display['display_options'] = [ + 'path' => 'test_route/%', + ]; + $this->pathPlugin->initDisplay($view, $display); + + $view_route_names = $this->pathPlugin->alterRoutes($collection); + $this->assertEquals(['test_id.page_1' => 'test_route'], $view_route_names); + + // Ensure that the test_route is overridden. + $route = $collection->get('test_route'); + $this->assertInstanceOf('\Symfony\Component\Routing\Route', $route); + $this->assertEquals('test_id', $route->getDefault('view_id')); + $this->assertEquals('page_1', $route->getDefault('display_id')); + $this->assertEquals(['taxonomy_term' => 'entity:entity_test'], $route->getOption('parameters')); + // Ensure that the path did not changed and placeholders are respected kk. + $this->assertEquals('/test_route/{parameter}', $route->getPath()); + $this->assertEquals(['arg_0' => 'parameter'], $route->getOption('_view_argument_map')); + } + + /** * Tests altering routes with optional parameters in the overridden route. */ public function testAlterRoutesWithOptionalParameters() {