From e5424fb2603c8e17189eba3cb8c6e8cb912ae4d7 Mon Sep 17 00:00:00 2001 From: webchick <drupal@webchick.net> Date: Sun, 4 Oct 2015 23:34:30 -0700 Subject: [PATCH] Issue #2410497 by alvar0hurtad0, sdstyles, hussainweb, webchick, jhodgdon, mpdonadio: Update remaining url() and _l() calls in comments/documentation --- .../lib/Drupal/Core/Language/language.api.php | 4 +-- .../lib/Drupal/Core/Menu/LocalTaskManager.php | 11 +++---- core/lib/Drupal/Core/Menu/menu.api.php | 15 ++++++---- core/lib/Drupal/Core/Render/Element/Link.php | 11 +++---- .../Core/Routing/UrlGeneratorInterface.php | 8 +++-- .../src/Tests/LanguageSwitchingTest.php | 8 ++--- .../src/Controller/LanguageTestController.php | 3 +- .../Field/FieldFormatter/LinkFormatter.php | 3 ++ .../FieldFormatter/LinkSeparateFormatter.php | 3 ++ core/modules/link/src/Tests/LinkFieldTest.php | 4 +-- core/modules/simpletest/src/TestBase.php | 3 +- .../system/src/Tests/Common/UrlTest.php | 29 ++++++++++--------- .../system/src/Tests/Theme/FunctionsTest.php | 5 ++-- .../src/Controller/CommonTestController.php | 3 +- .../Plugin/views/field/FieldPluginBase.php | 6 ++-- .../views/src/Tests/ViewStorageTest.php | 3 +- 16 files changed, 71 insertions(+), 48 deletions(-) diff --git a/core/lib/Drupal/Core/Language/language.api.php b/core/lib/Drupal/Core/Language/language.api.php index 8facf78cc961..f0dc2f760781 100644 --- a/core/lib/Drupal/Core/Language/language.api.php +++ b/core/lib/Drupal/Core/Language/language.api.php @@ -159,8 +159,8 @@ * Perform alterations on language switcher links. * * A language switcher link may need to point to a different path or use a - * translated link text before going through _l(), which will just handle the - * path aliases. + * translated link text before going through the link generator, which will + * just handle the path aliases. * * @param $links * Nested array of links keyed by language code. diff --git a/core/lib/Drupal/Core/Menu/LocalTaskManager.php b/core/lib/Drupal/Core/Menu/LocalTaskManager.php index 4cacddb9a58d..512ccfea9859 100644 --- a/core/lib/Drupal/Core/Menu/LocalTaskManager.php +++ b/core/lib/Drupal/Core/Menu/LocalTaskManager.php @@ -267,15 +267,16 @@ public function getLocalTasksForRoute($route_name) { foreach ($children[$parent] as $plugin_id => $task_info) { $plugin = $this->createInstance($plugin_id); $this->instances[$route_name][$level][$plugin_id] = $plugin; - // Normally, _l() compares the href of every link with the current - // path and sets the active class accordingly. But the parents of - // the current local task may be on a different route in which - // case we have to set the class manually by flagging it active. + // Normally, the link generator compares the href of every link with + // the current path and sets the active class accordingly. But the + // parents of the current local task may be on a different route in + // which case we have to set the class manually by flagging it + // active. if (!empty($parents[$plugin_id]) && $route_name != $task_info['route_name']) { $plugin->setActive(); } if (isset($children[$plugin_id])) { - // This tab has visible children + // This tab has visible children. $next_parent = $plugin_id; } } diff --git a/core/lib/Drupal/Core/Menu/menu.api.php b/core/lib/Drupal/Core/Menu/menu.api.php index 5d05634d0464..defec19b2930 100644 --- a/core/lib/Drupal/Core/Menu/menu.api.php +++ b/core/lib/Drupal/Core/Menu/menu.api.php @@ -256,8 +256,9 @@ * this menu item (as a result of other properties), then the menu link is * always expanded, equivalent to its 'always expanded' checkbox being set * in the UI. - * - options: (optional) An array of options to be passed to _l() when - * generating a link from this menu item. + * - options: (optional) An array of options to be passed to + * \Drupal\Core\Utility\LinkGeneratorInterface::generate() when generating + * a link from this menu item. * * @ingroup menu */ @@ -289,7 +290,8 @@ function hook_menu_links_discovered_alter(&$links) { * - #link: An associative array containing: * - title: The localized title of the link. * - url: a Url object. - * - localized_options: An array of options to pass to _l(). + * - localized_options: An array of options to pass to + * \Drupal\Core\Utility\LinkGeneratorInterface::generate(). * - #weight: The link's weight compared to other links. * - #active: Whether the link should be marked as 'active'. * @@ -437,9 +439,10 @@ function hook_system_breadcrumb_alter(\Drupal\Core\Breadcrumb\Breadcrumb &$bread * @param array $variables * An associative array of variables defining a link. The link may be either a * "route link" using \Drupal\Core\Utility\LinkGenerator::link(), which is - * exposed as the 'link_generator' service or a link generated by _l(). If the - * link is a "route link", 'route_name' will be set, otherwise 'path' will be - * set. The following keys can be altered: + * exposed as the 'link_generator' service or a link generated by + * \Drupal\Core\Utility\LinkGeneratorInterface::generate(). If the link is a + * "route link", 'route_name' will be set; otherwise, 'path' will be set. + * The following keys can be altered: * - text: The link text for the anchor tag. If the hook implementation * changes this text it needs to preserve the safeness of the original text. * Using t() or \Drupal\Component\Utility\SafeMarkup::format() with diff --git a/core/lib/Drupal/Core/Render/Element/Link.php b/core/lib/Drupal/Core/Render/Element/Link.php index 93ab6930d323..baa279e85493 100644 --- a/core/lib/Drupal/Core/Render/Element/Link.php +++ b/core/lib/Drupal/Core/Render/Element/Link.php @@ -38,17 +38,18 @@ public function getInfo() { * Doing so during pre_render gives modules a chance to alter the link parts. * * @param array $element - * A structured array whose keys form the arguments to _l(): - * - #title: The link text to pass as argument to _l(). + * A structured array whose keys form the arguments to + * \Drupal\Core\Utility\LinkGeneratorInterface::generate(): + * - #title: The link text. * - #url: The URL info either pointing to a route or a non routed path. - * - #options: (optional) An array of options to pass to _l() or the link - * generator. + * - #options: (optional) An array of options to pass to the link generator. * * @return array * The passed-in element containing a rendered link in '#markup'. */ public static function preRenderLink($element) { - // By default, link options to pass to _l() are normally set in #options. + // By default, link options to pass to the link generator are normally set + // in #options. $element += array('#options' => array()); // However, within the scope of renderable elements, #attributes is a valid // way to specify attributes, too. Take them into account, but do not override diff --git a/core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php b/core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php index f75940fb6ce2..97868c3fb643 100644 --- a/core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php +++ b/core/lib/Drupal/Core/Routing/UrlGeneratorInterface.php @@ -66,10 +66,12 @@ interface UrlGeneratorInterface extends VersatileGeneratorInterface { * - 'script': Added to the URL between the base path and the path prefix. * Defaults to empty string when clean URLs are in effect, and to * 'index.php/' when they are not. - * - 'entity_type': The entity type of the object that called _url(). Only - * set if _url() is invoked by Drupal\Core\Entity\Entity::uri(). + * - 'entity_type': The entity type of the object that called the URL + * generator. Only set if the URL generator is invoked by + * \Drupal\Core\Entity\Entity::uri(). * - 'entity': The entity object (such as a node) for which the URL is being - * generated. Only set if _url() is invoked by Drupal\Core\Entity\Entity::uri(). + * generated. Only set if the URL generator is invoked by + * \Drupal\Core\Entity\Entity::uri(). * @param bool $collect_bubbleable_metadata * (optional) Defaults to FALSE. When TRUE, both the generated URL and its * associated bubbleable metadata are returned. diff --git a/core/modules/language/src/Tests/LanguageSwitchingTest.php b/core/modules/language/src/Tests/LanguageSwitchingTest.php index f435f2682c9f..3bd64672cc80 100644 --- a/core/modules/language/src/Tests/LanguageSwitchingTest.php +++ b/core/modules/language/src/Tests/LanguageSwitchingTest.php @@ -292,7 +292,7 @@ protected function doTestLanguageLinkActiveClassAuthenticated() { $function_name = '#type link'; $path = 'language_test/type-link-active-class'; - // Test links generated by _l() on an English page. + // Test links generated by the link generator on an English page. $current_language = 'English'; $this->drupalGet($path); @@ -317,7 +317,7 @@ protected function doTestLanguageLinkActiveClassAuthenticated() { $this->assertIdentical($settings['path']['isFront'], FALSE, 'drupalSettings.path.isFront is set correctly to allow drupal.active-link to mark the correct links as active.'); $this->assertIdentical($settings['path']['currentLanguage'], 'en', 'drupalSettings.path.currentLanguage is set correctly to allow drupal.active-link to mark the correct links as active.'); - // Test links generated by _l() on a French page. + // Test links generated by the link generator on a French page. $current_language = 'French'; $this->drupalGet('fr/language_test/type-link-active-class'); @@ -353,7 +353,7 @@ protected function doTestLanguageLinkActiveClassAnonymous() { $this->drupalLogout(); - // Test links generated by _l() on an English page. + // Test links generated by the link generator on an English page. $current_language = 'English'; $this->drupalGet('language_test/type-link-active-class'); @@ -372,7 +372,7 @@ protected function doTestLanguageLinkActiveClassAnonymous() { $links = $this->xpath('//a[@id = :id and not(contains(@class, :class))]', array(':id' => 'fr_link', ':class' => 'is-active')); $this->assertTrue(isset($links[0]), t('A link generated by :function to the current :language page with langcode :langcode is NOT marked active.', array(':function' => $function_name, ':language' => $current_language, ':langcode' => $langcode))); - // Test links generated by _l() on a French page. + // Test links generated by the link generator on a French page. $current_language = 'French'; $this->drupalGet('fr/language_test/type-link-active-class'); 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 c783d3a99c67..b494bb347caa 100644 --- a/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php +++ b/core/modules/language/tests/language_test/src/Controller/LanguageTestController.php @@ -71,7 +71,8 @@ public function testEntity(ConfigurableLanguageInterface $configurable_language) /** * Returns links to the current page with different langcodes. * - * Using #type 'link' causes these links to be rendered with _l(). + * Using #type 'link' causes these links to be rendered with the link + * generator. */ public function typeLinkActiveClass() { // We assume that 'en' and 'fr' have been configured. diff --git a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php index 0bfabb5ea774..aeb208cdaa43 100644 --- a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkFormatter.php @@ -188,6 +188,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // If the title field value is available, use it for the link text. if (empty($settings['url_only']) && !empty($item->title)) { + // Unsanitized token replacement here because the entire link title + // gets auto-escaped during link generation in + // \Drupal\Core\Utility\LinkGenerator::generate(). $link_title = \Drupal::token()->replace($item->title, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]); } diff --git a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php index c6cf2123ae01..f13ea87093ff 100644 --- a/core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php +++ b/core/modules/link/src/Plugin/Field/FieldFormatter/LinkSeparateFormatter.php @@ -54,6 +54,9 @@ public function viewElements(FieldItemListInterface $items, $langcode) { // If the link text field value is available, use it for the text. if (empty($settings['url_only']) && !empty($item->title)) { + // Unsanitized token replacement here because the entire link title + // gets auto-escaped during link generation in + // \Drupal\Core\Utility\LinkGenerator::generate(). $link_title = \Drupal::token()->replace($item->title, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]); } diff --git a/core/modules/link/src/Tests/LinkFieldTest.php b/core/modules/link/src/Tests/LinkFieldTest.php index 8247b60456fb..4b61f730a021 100644 --- a/core/modules/link/src/Tests/LinkFieldTest.php +++ b/core/modules/link/src/Tests/LinkFieldTest.php @@ -389,8 +389,8 @@ function testLinkFormatter() { // Verify that the link is output according to the formatter settings. // Not using generatePermutations(), since that leads to 32 cases, which // would not test actual link field formatter functionality but rather - // _l() and options/attributes. Only 'url_plain' has a dependency on - // 'url_only', so we have a total of ~10 cases. + // the link generator and options/attributes. Only 'url_plain' has a + // dependency on 'url_only', so we have a total of ~10 cases. $options = array( 'trim_length' => array(NULL, 6), 'rel' => array(NULL, 'nofollow'), diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index 65edaeb792a2..9bf67aad3f79 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -907,7 +907,8 @@ protected function verbose($message) { $verbose_filename = $this->verboseClassName . '-' . $this->verboseId . '-' . $this->testId . '.html'; if (file_put_contents($this->verboseDirectory . '/' . $verbose_filename, $message)) { $url = $this->verboseDirectoryUrl . '/' . $verbose_filename; - // Not using _l() to avoid invoking the theme system, so that unit tests + // Not using \Drupal\Core\Utility\LinkGeneratorInterface::generate() + // to avoid invoking the theme system, so that unit tests // can use verbose() as well. $url = '<a href="' . $url . '" target="_blank">Verbose message</a>'; $this->error($url, 'User notice'); diff --git a/core/modules/system/src/Tests/Common/UrlTest.php b/core/modules/system/src/Tests/Common/UrlTest.php index 7584c10c0d61..f52d02202cb4 100644 --- a/core/modules/system/src/Tests/Common/UrlTest.php +++ b/core/modules/system/src/Tests/Common/UrlTest.php @@ -17,8 +17,9 @@ /** * Confirm that \Drupal\Core\Url, * \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), - * \Drupal\Component\Utility\UrlHelper::buildQuery(), and _l() work correctly - * with various input. + * \Drupal\Component\Utility\UrlHelper::buildQuery(), and + * \Drupal\Core\Utility\LinkGeneratorInterface::generate() + * work correctly with various input. * * @group Common */ @@ -36,7 +37,7 @@ function testLinkXSS() { $encoded_path = "3CSCRIPT%3Ealert%28%27XSS%27%29%3C/SCRIPT%3E"; $link = \Drupal::l($text, Url::fromUserInput('/' . $path)); - $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by _l().', array('@path' => $path))); + $this->assertTrue(strpos($link, $encoded_path) !== FALSE && strpos($link, $path) === FALSE, format_string('XSS attack @path was filtered by \Drupal\Core\Utility\LinkGeneratorInterface::generate().', array('@path' => $path))); // Test \Drupal\Core\Url. $link = Url::fromUri('base:' . $path)->toString(); @@ -102,7 +103,8 @@ function testLinkAttributes() { $rendered = $renderer->renderRoot($hreflang_override_link); $this->assertTrue($this->hasAttribute('hreflang', $rendered, 'foo'), format_string('hreflang attribute with value @hreflang is present on a rendered link when @hreflang is provided in the render array.', array('@hreflang' => 'foo'))); - // Test the active class in links produced by _l() and #type 'link'. + // Test the active class in links produced by + // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. $options_no_query = array(); $options_query = array( 'query' => array( @@ -122,23 +124,24 @@ function testLinkAttributes() { $this->drupalGet($path, $options_no_query); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active')); - $this->assertTrue(isset($links[0]), 'A link generated by _l() to the current page is marked active.'); + $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page is marked active.'); $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active')); - $this->assertTrue(isset($links[0]), 'A link generated by _l() to the current page with a query string when the current page has no query string is not marked active.'); + $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string when the current page has no query string is not marked active.'); $this->drupalGet($path, $options_query); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query)->toString(), ':class' => 'is-active')); - $this->assertTrue(isset($links[0]), 'A link generated by _l() to the current page with a query string that matches the current query string is marked active.'); + $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that matches the current query string is marked active.'); $links = $this->xpath('//a[@href = :href and contains(@class, :class)]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_query_reverse)->toString(), ':class' => 'is-active')); - $this->assertTrue(isset($links[0]), 'A link generated by _l() to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.'); + $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page with a query string that has matching parameters to the current query string but in a different order is marked active.'); $links = $this->xpath('//a[@href = :href and not(contains(@class, :class))]', array(':href' => Url::fromRoute('common_test.l_active_class', [], $options_no_query)->toString(), ':class' => 'is-active')); - $this->assertTrue(isset($links[0]), 'A link generated by _l() to the current page without a query string when the current page has a query string is not marked active.'); + $this->assertTrue(isset($links[0]), 'A link generated by the link generator to the current page without a query string when the current page has a query string is not marked active.'); - // Test adding a custom class in links produced by _l() and #type 'link'. - // Test _l(). + // Test adding a custom class in links produced by + // \Drupal\Core\Utility\LinkGeneratorInterface::generate() and #type 'link'. + // Test the link generator. $class_l = $this->randomMachineName(); $link_l = \Drupal::l($this->randomMachineName(), new Url('<current>', [], ['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))); @@ -166,10 +169,10 @@ function testLinkRenderArrayText() { /** @var \Drupal\Core\Render\RendererInterface $renderer */ $renderer = $this->container->get('renderer'); - // Build a link with _l() for reference. + // Build a link with the link generator for reference. $l = \Drupal::l('foo', Url::fromUri('https://www.drupal.org')); - // Test a renderable array passed to _l(). + // Test a renderable array passed to the link generator. $renderer->executeInRenderContext(new RenderContext(), function() use ($renderer, $l) { $renderable_text = array('#markup' => 'foo'); $l_renderable_text = \Drupal::l($renderable_text, Url::fromUri('https://www.drupal.org')); diff --git a/core/modules/system/src/Tests/Theme/FunctionsTest.php b/core/modules/system/src/Tests/Theme/FunctionsTest.php index 44f450775ecb..5e314223b6d5 100644 --- a/core/modules/system/src/Tests/Theme/FunctionsTest.php +++ b/core/modules/system/src/Tests/Theme/FunctionsTest.php @@ -172,8 +172,9 @@ function testItemList() { * Tests links.html.twig. */ function testLinks() { - // Turn off the query for the _l() function to compare the active - // link correctly. + // Turn off the query for the + // \Drupal\Core\Utility\LinkGeneratorInterface::generate() method to compare + // the active link correctly. $original_query = \Drupal::request()->query->all(); \Drupal::request()->query->replace(array()); // Verify that empty variables produce no output. 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 cbe4b142bdf7..383be8ff1978 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 @@ -19,7 +19,8 @@ class CommonTestController { /** * Returns links to the current page, with and without query strings. * - * Using #type 'link' causes these links to be rendered with _l(). + * Using #type 'link' causes these links to be rendered with the link + * generator. */ public function typeLinkActiveClass() { return array( diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index cc2a15178168..7cb05a2aa654 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -1271,7 +1271,8 @@ public function renderText($alter) { $more_link_path = $this->options['alter']['more_link_path']; $more_link_path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($more_link_path, $tokens))); - // Make sure that paths which were run through _url() work as well. + // Make sure that paths which were run through URL generation work as + // well. $base_path = base_path(); // Checks whether the path starts with the base_path. if (strpos($more_link_path, $base_path) === 0) { @@ -1501,7 +1502,8 @@ protected function renderAsLink($alter, $text, $tokens) { // parsed values. if (isset($alter['query'])) { // Convert the query to a string, perform token replacement, and then - // convert back to an array form for _l(). + // convert back to an array form for + // \Drupal\Core\Utility\LinkGeneratorInterface::generate(). $options['query'] = UrlHelper::buildQuery($alter['query']); $options['query'] = $this->viewsTokenReplace($options['query'], $tokens); $query = array(); diff --git a/core/modules/views/src/Tests/ViewStorageTest.php b/core/modules/views/src/Tests/ViewStorageTest.php index 357bfd8d7217..08432c4fb9be 100644 --- a/core/modules/views/src/Tests/ViewStorageTest.php +++ b/core/modules/views/src/Tests/ViewStorageTest.php @@ -185,7 +185,8 @@ protected function displayTests() { * Tests the display related functions like getDisplaysList(). */ protected function displayMethodTests() { - // Enable the system module so _l() can work using url_alias table. + // Enable the system module so the link generator can work using url_alias + // table. $this->installSchema('system', 'url_alias'); $config['display'] = array( -- GitLab