diff --git a/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php b/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php index cea8fba1bc050d3c378218d9db00602f47e5bbfd..cf585ce4f0db58950a4796309afb0dc30cd76fd1 100644 --- a/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php +++ b/core/modules/views/src/Tests/Plugin/StyleSummaryTest.php @@ -20,7 +20,7 @@ class StyleSummaryTest extends ViewTestBase { /** * {@inheritdoc} */ - public static $modules = ['entity_test']; + public static $modules = ['entity_test', 'views_ui']; /** * {@inheritdoc} @@ -48,6 +48,9 @@ protected function setUp($import_test_views = TRUE) { $entity->save(); } } + + $views_user = $this->drupalCreateUser(['administer views']); + $this->drupalLogin($views_user); } /** @@ -72,6 +75,71 @@ public function testSummaryView() { $this->clickLink('type1'); $entries = $this->cssSelect('div.view-content div.views-row'); $this->assertEqual(2, count($entries)); + + // Add a base path to the summary settings. + $edit = [ + 'options[summary][options][default_summary][base_path]' => 'test-summary', + ]; + $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + + // Test that the links still work. + $this->drupalGet('test-summary'); + $this->clickLink('type1'); + $entries = $this->cssSelect('div.view-content div.views-row'); + $this->assertEqual(2, count($entries)); + + // Change the summary display to an unformatted list displaying 3 items. + $edit = [ + 'options[summary][format]' => 'unformatted_summary', + 'options[summary][options][unformatted_summary][override]' => '1', + 'options[summary][options][unformatted_summary][items_per_page]' => '3', + ]; + $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + + $this->drupalGet('admin/structure/views/nojs/handler/test_summary/page_1/argument/type'); + $this->drupalGet('test-summary'); + + $summary_list = $this->cssSelect('.views-summary-unformatted'); + $this->assertEqual(3, count($summary_list)); + + foreach ($summary_list as $summary_list_item) { + $this->assertEqual('(5)', trim((string) $summary_list_item)); + } + + $summary_links = $this->cssSelect('.views-summary-unformatted a'); + $this->assertEqual(3, count($summary_links)); + foreach ($summary_links as $index => $summary_link) { + $this->assertEqual('type' . $index, trim((string) $summary_link)); + } + + $this->clickLink('type1'); + $entries = $this->cssSelect('div.view-content div.views-row'); + $this->assertEqual(2, count($entries)); + + // Add a base path to the summary settings. + $edit = [ + 'options[summary][options][unformatted_summary][base_path]' => 'test-summary', + ]; + $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + + // Test that the links still work. + $this->drupalGet('test-summary'); + $this->clickLink('type1'); + $entries = $this->cssSelect('div.view-content div.views-row'); + $this->assertEqual(2, count($entries)); + + // Set base_path to an unknown path and test that the links lead to the + // front page. + $edit = [ + 'options[summary][options][unformatted_summary][base_path]' => 'unknown-path', + ]; + $this->drupalPostForm('admin/structure/views/nojs/handler/test_summary/page_1/argument/type', $edit, t('Apply')); + $this->drupalPostForm(NULL, [], t('Save')); + $this->drupalGet('test-summary'); + $this->assertLinkByHref('/'); } } diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml index daf92fa2a27bd01e71355f20878e62145b2f9c1c..3784bd960f0cd28ebf1d66d56182d66b14503f6a 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_summary.yml @@ -75,6 +75,7 @@ display: specify_validation: true plugin_id: string entity_type: entity_test + admin_label: type fields: id: id: id diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index c14416f391f5b0075173e4c55a7c888b10f14373..94c519e71d7f5a0cce883b59fc3edcb28915187c 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -277,11 +277,29 @@ function template_preprocess_views_view_summary(&$variables) { if (!empty($argument->options['summary_options']['base_path'])) { $base_path = $argument->options['summary_options']['base_path']; - $tokens = $this->getArgumentsTokens(); - $base_path = $this->viewsTokenReplace($base_path, $tokens); + $tokens = $view->getDisplay()->getArgumentsTokens(); + $base_path = $argument->globalTokenReplace($base_path, $tokens); // @todo Views should expect and store a leading /. See: // https://www.drupal.org/node/2423913 $url = Url::fromUserInput('/' . $base_path); + try { + /** @var \Symfony\Component\Routing\Route $route */ + $route_name = $url->getRouteName(); + $route = \Drupal::service('router.route_provider')->getRouteByName($route_name); + + $route_variables = $route->compile()->getVariables(); + $parameters = $url->getRouteParameters(); + + foreach ($route_variables as $variable_name) { + $parameters[$variable_name] = array_shift($args); + } + + $url->setRouteParameters($parameters); + } + catch (Exception $e) { + // If the given route doesn't exist, default to <front> + $url = Url::fromRoute('<front>'); + } } else { $url = $view->getUrl($args)->setOptions($url_options); @@ -307,6 +325,7 @@ function template_preprocess_views_view_summary(&$variables) { * visually distinct. */ function template_preprocess_views_view_summary_unformatted(&$variables) { + /** @var \Drupal\views\ViewExecutable $view */ $view = $variables['view']; $argument = $view->argument[$view->build_info['summary_level']]; @@ -346,11 +365,27 @@ function template_preprocess_views_view_summary_unformatted(&$variables) { if (!empty($argument->options['summary_options']['base_path'])) { $base_path = $argument->options['summary_options']['base_path']; - $tokens = $this->getArgumentsTokens(); - $base_path = $this->viewsTokenReplace($base_path, $tokens); + $tokens = $view->getDisplay()->getArgumentsTokens(); + $base_path = $argument->globalTokenReplace($base_path, $tokens); // @todo Views should expect and store a leading /. See: // https://www.drupal.org/node/2423913 $url = Url::fromUserInput('/' . $base_path); + try { + /** @var \Symfony\Component\Routing\Route $route */ + $route = \Drupal::service('router.route_provider')->getRouteByName($url->getRouteName()); + $route_variables = $route->compile()->getVariables(); + $parameters = $url->getRouteParameters(); + + foreach ($route_variables as $variable_name) { + $parameters[$variable_name] = array_shift($args); + } + + $url->setRouteParameters($parameters); + } + catch (Exception $e) { + // If the given route doesn't exist, default to <front> + $url = Url::fromRoute('<front>'); + } } else { $url = $view->getUrl($args)->setOptions($url_options);