From 4e5c1c2befd9196d9a55cda3e7050aaad3b529c7 Mon Sep 17 00:00:00 2001 From: catch Date: Thu, 2 Oct 2014 13:39:56 +0200 Subject: [PATCH] Issue #2345753 by tim.plunkett, dawehner, Wim Leers, damiankloip: Remove url(current_path()) and url(NULL) with and . --- core/core.services.yml | 13 ++--- core/includes/pager.inc | 12 ++--- core/includes/tablesort.inc | 5 +- .../PathProcessor/PathProcessorCurrent.php | 31 ------------ .../Core/PathProcessor/PathProcessorNone.php | 27 ----------- .../RouteProcessor/RouteProcessorCurrent.php | 48 +++++++++++++++++++ .../RouteProcessor/RouteProcessorNone.php | 26 ++++++++++ core/lib/Drupal/Core/Routing/UrlGenerator.php | 3 +- .../src/Tests/Views/IntegrationTest.php | 2 +- .../src/Controller/LanguageTestController.php | 6 +-- core/modules/path/src/Form/PathFormBase.php | 4 +- .../system/src/Form/SiteInformationForm.php | 6 +-- .../system/src/Tests/Common/UrlTest.php | 7 +-- .../RouteProcessorCurrentIntegrationTest.php} | 24 ++++++++-- .../RouteProcessorNoneIntegrationTest.php} | 24 ++++++++-- .../src/Controller/CommonTestController.php | 6 +-- .../Plugin/views/display/PathPluginBase.php | 5 +- .../Plugin/views/wizard/WizardPluginBase.php | 5 +- .../views/src/Tests/ViewUnitTestBase.php | 1 + core/modules/views/views.theme.inc | 16 ++++--- .../Tests/Core/Routing/UrlGeneratorTest.php | 8 ++-- 21 files changed, 168 insertions(+), 111 deletions(-) delete mode 100644 core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php delete mode 100644 core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php create mode 100644 core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php create mode 100644 core/lib/Drupal/Core/RouteProcessor/RouteProcessorNone.php rename core/modules/system/src/Tests/{PathProcessor/PathProcessorCurrentIntegrationTest.php => RouteProcessor/RouteProcessorCurrentIntegrationTest.php} (69%) rename core/modules/system/src/Tests/{PathProcessor/PathProcessorNoneIntegrationTest.php => RouteProcessor/RouteProcessorNoneIntegrationTest.php} (73%) diff --git a/core/core.services.yml b/core/core.services.yml index 7abab67b453..3c271304a32 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -824,14 +824,15 @@ services: - { name: path_processor_inbound, priority: 200 } - { name: path_processor_outbound, priority: 200 } arguments: ['@config.factory'] - path_processor_none: - class: Drupal\Core\PathProcessor\PathProcessorNone + route_processor_none: + class: Drupal\Core\RouteProcessor\RouteProcessorNone tags: - - { name: path_processor_outbound, priority: 200 } - path_processor_current: - class: Drupal\Core\PathProcessor\PathProcessorCurrent + - { name: route_processor_outbound, priority: 200 } + route_processor_current: + class: Drupal\Core\RouteProcessor\RouteProcessorCurrent + arguments: ['@current_route_match'] tags: - - { name: path_processor_outbound, priority: 200 } + - { name: route_processor_outbound, priority: 200 } path_processor_alias: class: Drupal\Core\PathProcessor\PathProcessorAlias tags: diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 0dcfcb78c6d..05be5c8edde 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -211,15 +211,13 @@ function template_preprocess_pager(&$variables) { } // End of generation loop preparation. - $current_path = current_path(); - // Create the "first" and "previous" links if we are not on the first page. if ($pager_page_array[$element] > 0) { $items['first'] = array(); $options = array( 'query' => pager_query_add_page($parameters, $element, 0), ); - $items['first']['href'] = _url($current_path, $options); + $items['first']['href'] = \Drupal::url('', [], $options); if (isset($tags[0])) { $items['first']['text'] = $tags[0]; } @@ -228,7 +226,7 @@ function template_preprocess_pager(&$variables) { $options = array( 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1), ); - $items['previous']['href'] = _url($current_path, $options); + $items['previous']['href'] = \Drupal::url('', [], $options); if (isset($tags[1])) { $items['previous']['text'] = $tags[1]; } @@ -244,7 +242,7 @@ function template_preprocess_pager(&$variables) { $options = array( 'query' => pager_query_add_page($parameters, $element, $i - 1), ); - $items['pages'][$i]['href'] = _url($current_path, $options); + $items['pages'][$i]['href'] = \Drupal::url('', [], $options); if ($i == $pager_current) { $variables['current'] = $i; } @@ -261,7 +259,7 @@ function template_preprocess_pager(&$variables) { $options = array( 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1), ); - $items['next']['href'] = _url($current_path, $options); + $items['next']['href'] = \Drupal::url('', [], $options); if (isset($tags[3])) { $items['next']['text'] = $tags[3]; } @@ -270,7 +268,7 @@ function template_preprocess_pager(&$variables) { $options = array( 'query' => pager_query_add_page($parameters, $element, $pager_max - 1), ); - $items['last']['href'] = _url($current_path, $options); + $items['last']['href'] = \Drupal::url('', [], $options); if (isset($tags[4])) { $items['last']['text'] = $tags[4]; } diff --git a/core/includes/tablesort.inc b/core/includes/tablesort.inc index 0c8d25b0839..0258a767422 100644 --- a/core/includes/tablesort.inc +++ b/core/includes/tablesort.inc @@ -3,6 +3,7 @@ use Drupal\Core\Database\Connection; use Drupal\Core\Database\Query\SelectExtender; use Drupal\Core\Database\Query\SelectInterface; +use Drupal\Core\Url; use Drupal\Component\Utility\UrlHelper; /** @@ -61,14 +62,14 @@ function tablesort_header(&$cell_content, array &$cell_attributes, array $header $ts['sort'] = 'asc'; $image = ''; } - $cell_content = _l($cell_content . $image, current_path(), array( + $cell_content = \Drupal::l($cell_content . $image, new Url('', [], [ 'attributes' => array('title' => $title), 'query' => array_merge($ts['query'], array( 'sort' => $ts['sort'], 'order' => $cell_content, )), 'html' => TRUE, - )); + ])); unset($cell_attributes['field'], $cell_attributes['sort']); } diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php deleted file mode 100644 index 8931462161d..00000000000 --- a/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php +++ /dev/null @@ -1,31 +0,0 @@ -. - */ -class PathProcessorCurrent implements OutboundPathProcessorInterface { - - /** - * {@inheritdoc} - */ - public function processOutbound($path, &$options = array(), Request $request = NULL) { - if ($path == '%3Ccurrent%3E' && $request) { - $request_uri = $request->getRequestUri(); - - $current_base_path = $request->getBasePath() . '/'; - return substr($request_uri, strlen($current_base_path)); - } - return $path; - } - -} - diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php deleted file mode 100644 index 2808ed1d671..00000000000 --- a/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php +++ /dev/null @@ -1,27 +0,0 @@ -. - */ -class PathProcessorNone implements OutboundPathProcessorInterface { - - /** - * {@inheritdoc} - */ - public function processOutbound($path, &$options = array(), Request $request = NULL) { - if ($path == '%3Cnone%3E') { - return ''; - } - return $path; - } - -} diff --git a/core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php b/core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php new file mode 100644 index 00000000000..e4bc705df71 --- /dev/null +++ b/core/lib/Drupal/Core/RouteProcessor/RouteProcessorCurrent.php @@ -0,0 +1,48 @@ +. + */ +class RouteProcessorCurrent implements OutboundRouteProcessorInterface { + + /** + * The current route match. + * + * @var \Drupal\Core\Routing\RouteMatchInterface + */ + protected $routeMatch; + + /** + * Constructs a new RouteProcessorCurrent. + * + * @param \Drupal\Core\Routing\RouteMatchInterface $route_match + * The current route match. + */ + public function __construct(RouteMatchInterface $route_match) { + $this->routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public function processOutbound($route_name, Route $route, array &$parameters) { + if (($route_name === '') && ($current_route = $this->routeMatch->getRouteObject())) { + $route->setPath($current_route->getPath()); + $route->setRequirements($current_route->getRequirements()); + $route->setOptions($current_route->getOptions()); + $route->setDefaults($current_route->getDefaults()); + $parameters = array_merge($parameters, $this->routeMatch->getRawParameters()->all()); + } + } + +} diff --git a/core/lib/Drupal/Core/RouteProcessor/RouteProcessorNone.php b/core/lib/Drupal/Core/RouteProcessor/RouteProcessorNone.php new file mode 100644 index 00000000000..91ea6f1352f --- /dev/null +++ b/core/lib/Drupal/Core/RouteProcessor/RouteProcessorNone.php @@ -0,0 +1,26 @@ +. + */ +class RouteProcessorNone implements OutboundRouteProcessorInterface { + + /** + * {@inheritdoc} + */ + public function processOutbound($route_name, Route $route, array &$parameters) { + if ($route_name === '') { + $route->setPath(''); + } + } + +} diff --git a/core/lib/Drupal/Core/Routing/UrlGenerator.php b/core/lib/Drupal/Core/Routing/UrlGenerator.php index ef8d9c9681f..4657fdf2c91 100644 --- a/core/lib/Drupal/Core/Routing/UrlGenerator.php +++ b/core/lib/Drupal/Core/Routing/UrlGenerator.php @@ -101,6 +101,7 @@ class UrlGenerator extends ProviderBasedGenerator implements UrlGeneratorInterfa */ public function getPathFromRoute($name, $parameters = array()) { $route = $this->getRoute($name); + $this->processRoute($name, $route, $parameters); $path = $this->getInternalPathFromRoute($route, $parameters); // Router-based paths may have a querystring on them but Drupal paths may // not have one, so remove any ? and anything after it. For generate() this @@ -358,7 +359,7 @@ class UrlGenerator extends ProviderBasedGenerator implements UrlGeneratorInterfa if ($name instanceof SymfonyRoute) { $route = $name; } - elseif (NULL === $route = $this->provider->getRouteByName($name)) { + elseif (NULL === $route = clone $this->provider->getRouteByName($name)) { throw new RouteNotFoundException(sprintf('Route "%s" does not exist.', $name)); } return $route; diff --git a/core/modules/aggregator/src/Tests/Views/IntegrationTest.php b/core/modules/aggregator/src/Tests/Views/IntegrationTest.php index 8b6edaad667..e4bb5fc2111 100644 --- a/core/modules/aggregator/src/Tests/Views/IntegrationTest.php +++ b/core/modules/aggregator/src/Tests/Views/IntegrationTest.php @@ -23,7 +23,7 @@ class IntegrationTest extends ViewUnitTestBase { * * @var array */ - public static $modules = array('aggregator', 'aggregator_test_views', 'system', 'entity', 'field', 'options'); + public static $modules = array('aggregator', 'aggregator_test_views', 'system', 'entity', 'field', 'options', 'user'); /** * Views used by this test. diff --git a/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php b/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php index 2935b7dac8a..a5cb033a3be 100644 --- a/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php +++ b/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php @@ -62,7 +62,7 @@ class LanguageTestController implements ContainerInjectionInterface { 'no_language' => array( '#type' => 'link', '#title' => t('Link to the current path with no langcode provided.'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'attributes' => array( 'id' => 'no_lang_link', @@ -73,7 +73,7 @@ class LanguageTestController implements ContainerInjectionInterface { 'fr' => array( '#type' => 'link', '#title' => t('Link to a French version of the current path.'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'language' => $languages['fr'], 'attributes' => array( @@ -85,7 +85,7 @@ class LanguageTestController implements ContainerInjectionInterface { 'en' => array( '#type' => 'link', '#title' => t('Link to an English version of the current path.'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'language' => $languages['en'], 'attributes' => array( diff --git a/core/modules/path/src/Form/PathFormBase.php b/core/modules/path/src/Form/PathFormBase.php index ecb970c73d7..f4d930957b6 100644 --- a/core/modules/path/src/Form/PathFormBase.php +++ b/core/modules/path/src/Form/PathFormBase.php @@ -95,7 +95,7 @@ abstract class PathFormBase extends FormBase { '#maxlength' => 255, '#size' => 45, '#description' => $this->t('Specify the existing path you wish to alias. For example: node/28, forum/1, taxonomy/term/1.'), - '#field_prefix' => _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => $this->url('', [], ['absolute' => TRUE]), '#required' => TRUE, ); $form['alias'] = array( @@ -105,7 +105,7 @@ abstract class PathFormBase extends FormBase { '#maxlength' => 255, '#size' => 45, '#description' => $this->t('Specify an alternative path by which this data can be accessed. For example, type "about" when writing an about page. Use a relative path and don\'t add a trailing slash or the URL alias won\'t work.'), - '#field_prefix' => _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => $this->url('', [], ['absolute' => TRUE]), '#required' => TRUE, ); diff --git a/core/modules/system/src/Form/SiteInformationForm.php b/core/modules/system/src/Form/SiteInformationForm.php index 7ae14cf87db..f51bd983b2f 100644 --- a/core/modules/system/src/Form/SiteInformationForm.php +++ b/core/modules/system/src/Form/SiteInformationForm.php @@ -114,7 +114,7 @@ class SiteInformationForm extends ConfigFormBase { '#default_value' => $front_page, '#size' => 40, '#description' => t('Optionally, specify a relative URL to display as the front page. Leave blank to display the default front page.'), - '#field_prefix' => _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => $this->url('', [], ['absolute' => TRUE]), ); $form['error_page'] = array( '#type' => 'details', @@ -127,7 +127,7 @@ class SiteInformationForm extends ConfigFormBase { '#default_value' => $site_config->get('page.403'), '#size' => 40, '#description' => t('This page is displayed when the requested document is denied to the current user. Leave blank to display a generic "access denied" page.'), - '#field_prefix' => _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => $this->url('', [], ['absolute' => TRUE]), ); $form['error_page']['site_404'] = array( '#type' => 'textfield', @@ -135,7 +135,7 @@ class SiteInformationForm extends ConfigFormBase { '#default_value' => $site_config->get('page.404'), '#size' => 40, '#description' => t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'), - '#field_prefix' => _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => $this->url('', [], ['absolute' => TRUE]), ); return parent::buildForm($form, $form_state); diff --git a/core/modules/system/src/Tests/Common/UrlTest.php b/core/modules/system/src/Tests/Common/UrlTest.php index 0097512dc37..3ac4bc4ebca 100644 --- a/core/modules/system/src/Tests/Common/UrlTest.php +++ b/core/modules/system/src/Tests/Common/UrlTest.php @@ -9,6 +9,7 @@ namespace Drupal\system\Tests\Common; use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Language\Language; +use Drupal\Core\Url; use Drupal\simpletest\WebTestBase; /** @@ -114,15 +115,15 @@ class UrlTest extends WebTestBase { // Test adding a custom class in links produced by _l() and #type 'link'. // Test _l(). $class_l = $this->randomMachineName(); - $link_l = _l($this->randomMachineName(), current_path(), array('attributes' => array('class' => array($class_l)))); - $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by _l()', array('@class' => $class_l))); + $link_l = \Drupal::l($this->randomMachineName(), new Url('', [], ['attributes' => ['class' => [$class_l]]])); + $this->assertTrue($this->hasAttribute('class', $link_l, $class_l), format_string('Custom class @class is present on link when requested by l()', array('@class' => $class_l))); // Test #type. $class_theme = $this->randomMachineName(); $type_link = array( '#type' => 'link', '#title' => $this->randomMachineName(), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'attributes' => array( 'class' => array($class_theme), diff --git a/core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php b/core/modules/system/src/Tests/RouteProcessor/RouteProcessorCurrentIntegrationTest.php similarity index 69% rename from core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php rename to core/modules/system/src/Tests/RouteProcessor/RouteProcessorCurrentIntegrationTest.php index 74388ed52f2..2d530826c30 100644 --- a/core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php +++ b/core/modules/system/src/Tests/RouteProcessor/RouteProcessorCurrentIntegrationTest.php @@ -2,19 +2,21 @@ /** * @file - * Contains \Drupal\system\Tests\PathProcessor\PathProcessorIntegrationTest. + * Contains \Drupal\system\Tests\RouteProcessor\RouteProcessorIntegrationTest. */ -namespace Drupal\system\Tests\PathProcessor; +namespace Drupal\system\Tests\RouteProcessor; use Drupal\simpletest\KernelTestBase; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Route; /** - * @see \Drupal\Core\PathProcessor\PathProcessorCurrent - * @group path_processor + * @see \Drupal\Core\RouteProcessor\RouteProcessorCurrent + * @group route_processor */ -class PathProcessorCurrentIntegrationTest extends KernelTestBase { +class RouteProcessorCurrentIntegrationTest extends KernelTestBase { /** * {@inheritdoc} @@ -46,6 +48,9 @@ class PathProcessorCurrentIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/subdir', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/subdir/', \Drupal::url('')); @@ -57,6 +62,9 @@ class PathProcessorCurrentIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/subdir/node/add', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/subdir/node/add', \Drupal::url('')); @@ -68,6 +76,9 @@ class PathProcessorCurrentIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/', \Drupal::url('')); @@ -79,6 +90,9 @@ class PathProcessorCurrentIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/node/add', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/node/add', \Drupal::url('')); diff --git a/core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php b/core/modules/system/src/Tests/RouteProcessor/RouteProcessorNoneIntegrationTest.php similarity index 73% rename from core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php rename to core/modules/system/src/Tests/RouteProcessor/RouteProcessorNoneIntegrationTest.php index 354131acd1e..1089498cc29 100644 --- a/core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php +++ b/core/modules/system/src/Tests/RouteProcessor/RouteProcessorNoneIntegrationTest.php @@ -2,19 +2,21 @@ /** * @file - * Contains \Drupal\system\Tests\PathProcessor\PathProcessorNoneIntegrationTest. + * Contains \Drupal\system\Tests\PathProcessor\RouteProcessorNoneIntegrationTest. */ -namespace Drupal\system\Tests\PathProcessor; +namespace Drupal\system\Tests\RouteProcessor; use Drupal\simpletest\KernelTestBase; +use Symfony\Cmf\Component\Routing\RouteObjectInterface; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\Routing\Route; /** - * @see \Drupal\Core\PathProcessor\PathProcessorNone - * @group path_processor + * @see \Drupal\Core\RouteProcessor\RouteProcessorNone + * @group route_processor */ -class PathProcessorNoneIntegrationTest extends KernelTestBase { +class RouteProcessorNoneIntegrationTest extends KernelTestBase { /** * {@inheritdoc} @@ -46,6 +48,9 @@ class PathProcessorNoneIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/subdir', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/subdir/', \Drupal::url('')); @@ -58,6 +63,9 @@ class PathProcessorNoneIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/subdir/node/add', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/subdir/', \Drupal::url('')); @@ -70,6 +78,9 @@ class PathProcessorNoneIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, ''); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/', \Drupal::url('')); @@ -82,6 +93,9 @@ class PathProcessorNoneIntegrationTest extends KernelTestBase { 'SERVER_NAME' => 'http://www.example.com', ]; $request = Request::create('/node/add', 'GET', [], [], [], $server); + $request->attributes->set(RouteObjectInterface::ROUTE_NAME, 'node.add'); + $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route('/node/add')); + $request_stack->push($request); $request_context->fromRequest($request); $this->assertEqual('/', \Drupal::url('')); diff --git a/core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php b/core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php index be4ce968a1c..161aabe32e2 100644 --- a/core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php +++ b/core/modules/system/tests/modules/common_test/src/Controller/CommonTestController.php @@ -25,7 +25,7 @@ class CommonTestController { 'no_query' => array( '#type' => 'link', '#title' => t('Link with no query string'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'set_active_class' => TRUE, ), @@ -33,7 +33,7 @@ class CommonTestController { 'with_query' => array( '#type' => 'link', '#title' => t('Link with a query string'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'query' => array( 'foo' => 'bar', @@ -45,7 +45,7 @@ class CommonTestController { 'with_query_reversed' => array( '#type' => 'link', '#title' => t('Link with the same query string in reverse order'), - '#href' => current_path(), + '#route_name' => '', '#options' => array( 'query' => array( 'one' => 'two', diff --git a/core/modules/views/src/Plugin/views/display/PathPluginBase.php b/core/modules/views/src/Plugin/views/display/PathPluginBase.php index ee8370ab678..f3d90cef762 100644 --- a/core/modules/views/src/Plugin/views/display/PathPluginBase.php +++ b/core/modules/views/src/Plugin/views/display/PathPluginBase.php @@ -9,6 +9,7 @@ namespace Drupal\views\Plugin\views\display; use Drupal\Core\Access\AccessManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\Core\State\StateInterface; use Drupal\Core\Routing\RouteCompiler; use Drupal\Core\Routing\RouteProviderInterface; @@ -27,6 +28,8 @@ use Symfony\Component\Routing\RouteCollection; */ abstract class PathPluginBase extends DisplayPluginBase implements DisplayRouterInterface { + use UrlGeneratorTrait; + /** * The route provider. * @@ -393,7 +396,7 @@ abstract class PathPluginBase extends DisplayPluginBase implements DisplayRouter '#title' => $this->t('Path'), '#description' => $this->t('This view will be displayed by visiting this path on your site. You may use "%" in your URL to represent values that will be used for contextual filters: For example, "node/%/feed". If needed you can even specify named route parameters like taxonomy/term/%taxonomy_term'), '#default_value' => $this->getOption('path'), - '#field_prefix' => '' . _url(NULL, array('absolute' => TRUE)), + '#field_prefix' => '' . $this->url('', [], ['absolute' => TRUE]), '#field_suffix' => '‎', '#attributes' => array('dir' => 'ltr'), // Account for the leading backslash. diff --git a/core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php b/core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php index c2a23dcea6c..c8a127b7aae 100644 --- a/core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php +++ b/core/modules/views/src/Plugin/views/wizard/WizardPluginBase.php @@ -9,6 +9,7 @@ namespace Drupal\views\Plugin\views\wizard; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Routing\UrlGeneratorTrait; use Drupal\views\Entity\View; use Drupal\views\Views; use Drupal\views_ui\ViewUI; @@ -38,6 +39,8 @@ use Drupal\views\Plugin\views\wizard\WizardInterface; */ abstract class WizardPluginBase extends PluginBase implements WizardInterface { + use UrlGeneratorTrait; + /** * The base table connected with the wizard. * @@ -218,7 +221,7 @@ abstract class WizardPluginBase extends PluginBase implements WizardInterface { public function buildForm(array $form, FormStateInterface $form_state) { $style_options = Views::fetchPluginNames('style', 'normal', array($this->base_table)); $feed_row_options = Views::fetchPluginNames('row', 'feed', array($this->base_table)); - $path_prefix = _url(NULL, array('absolute' => TRUE)); + $path_prefix = $this->url('', [], ['absolute' => TRUE]); // Add filters and sorts which apply to the view as a whole. $this->buildFilters($form, $form_state); diff --git a/core/modules/views/src/Tests/ViewUnitTestBase.php b/core/modules/views/src/Tests/ViewUnitTestBase.php index fa14ec930cb..ad5c39272ed 100644 --- a/core/modules/views/src/Tests/ViewUnitTestBase.php +++ b/core/modules/views/src/Tests/ViewUnitTestBase.php @@ -55,6 +55,7 @@ abstract class ViewUnitTestBase extends DrupalUnitTestBase { // The router table is required for router rebuilds. $this->installSchema('system', array('router')); + \Drupal::service('router.builder')->rebuild(); // Load the test dataset. $data_set = $this->dataSet(); diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 9ab4be03fd3..02f017569b1 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -10,6 +10,7 @@ use Drupal\Component\Utility\SafeMarkup; use Drupal\Component\Utility\String; use Drupal\Component\Utility\Xss; use Drupal\Core\Template\Attribute; +use Drupal\Core\Url; /** * Prepares variables for view templates. @@ -306,7 +307,11 @@ function template_preprocess_views_view_summary(&$variables) { } $active_urls = array( + // Force system path. + \Drupal::url('', [], ['alias' => TRUE]), _url(current_path(), array('alias' => TRUE)), // force system path + // Could be an alias. + \Drupal::url(''), _url(current_path()), // could be an alias ); $active_urls = array_combine($active_urls, $active_urls); @@ -366,9 +371,9 @@ function template_preprocess_views_view_summary_unformatted(&$variables) { $count = 0; $active_urls = array( // Force system path. - _url(current_path(), array('alias' => TRUE)), + \Drupal::url('', [], ['alias' => TRUE]), // Could be an alias. - _url(current_path()), + \Drupal::url(''), ); $active_urls = array_combine($active_urls, $active_urls); @@ -484,7 +489,7 @@ function template_preprocess_views_view_table(&$variables) { 'attributes' => array('title' => $title), 'query' => $query, ); - $variables['header'][$field]['content'] = _l($label, current_path(), $link_options); + $variables['header'][$field]['content'] = \Drupal::l($label, new Url('', [], $link_options)); } // Set up the header label class. @@ -1038,14 +1043,13 @@ function template_preprocess_views_mini_pager(&$variables) { // Current is the page we are currently paged to. $pager_current = $pager_page_array[$element] + 1; - $current_path = current_path(); $li_previous = array(); if ($pager_total[$element] > 1 && $pager_page_array[$element] > 0) { $li_previous = array( '#type' => 'link', '#title' => $tags[1], - '#href' => $current_path, + '#route_name' => '', '#options' => array( 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1), 'attributes' => array( @@ -1068,7 +1072,7 @@ function template_preprocess_views_mini_pager(&$variables) { $li_next = array( '#type' => 'link', '#title' => $tags[3], - '#href' => $current_path, + '#route_name' => '', '#options' => array( 'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1), 'attributes' => array( diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php index f5366f09938..740437245db 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php @@ -178,7 +178,7 @@ class UrlGeneratorTest extends UnitTestCase { $url = $this->generator->generate('test_1'); $this->assertEquals('/hello/world', $url); - $this->routeProcessorManager->expects($this->once()) + $this->routeProcessorManager->expects($this->exactly(2)) ->method('processOutbound') ->with($this->anything()); @@ -195,7 +195,7 @@ class UrlGeneratorTest extends UnitTestCase { * Tests URL generation in a subdirectory. */ public function testGetPathFromRouteWithSubdirectory() { - $this->routeProcessorManager->expects($this->never()) + $this->routeProcessorManager->expects($this->once()) ->method('processOutbound'); $path = $this->generator->getPathFromRoute('test_1'); @@ -209,7 +209,7 @@ class UrlGeneratorTest extends UnitTestCase { $url = $this->generator->generate('test_2', array('narf' => '5')); $this->assertEquals('/goodbye/cruel/world', $url); - $this->routeProcessorManager->expects($this->exactly(3)) + $this->routeProcessorManager->expects($this->exactly(4)) ->method('processOutbound') ->with($this->anything()); @@ -234,7 +234,7 @@ class UrlGeneratorTest extends UnitTestCase { * Tests URL generation from route with trailing start and end slashes. */ public function testGetPathFromRouteTrailing() { - $this->routeProcessorManager->expects($this->never()) + $this->routeProcessorManager->expects($this->once()) ->method('processOutbound'); $path = $this->generator->getPathFromRoute('test_3');