From 1299c3afcf6529e31e8f818978459aa2191f67a6 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 5 May 2014 16:14:38 +0100 Subject: [PATCH] Issue #2233619 by slashrsm, Jalandhar: Merge lookup functions in AliasManager. --- core/includes/common.inc | 2 +- core/includes/theme.inc | 2 +- .../AliasManagerCacheDecorator.php | 12 +- .../Core/Controller/ExceptionController.php | 4 +- core/lib/Drupal/Core/Path/AliasManager.php | 179 ++++++++---------- .../Core/Path/AliasManagerInterface.php | 31 ++- .../Core/PathProcessor/PathProcessorAlias.php | 4 +- .../Drupal/block/BlockAccessController.php | 2 +- .../Plugin/Field/FieldWidget/LinkWidget.php | 2 +- .../Drupal/locale/Tests/LocalePathTest.php | 4 +- .../lib/Drupal/menu_link/MenuLinkForm.php | 2 +- .../Drupal/path/Controller/PathController.php | 2 +- .../lib/Drupal/path/Form/PathFormBase.php | 4 +- .../Drupal/path/Tests/PathLanguageTest.php | 8 +- core/modules/shortcut/shortcut.module | 2 +- .../shortcut/src/ShortcutPathValue.php | 2 +- .../shortcut/src/Tests/ShortcutLinksTest.php | 2 +- core/modules/statistics/statistics.module | 2 +- .../system/Form/SiteInformationForm.php | 8 +- .../Drupal/system/Tests/Path/AliasTest.php | 22 +-- .../Tests/Path/UrlAlterFunctionalTest.php | 2 +- .../system/Tests/Routing/MockAliasManager.php | 15 +- .../Plugin/views/argument_default/Raw.php | 2 +- .../Tests/Plugin/argument_default/RawTest.php | 4 +- core/modules/views_ui/views_ui.module | 2 +- .../Tests/Core/Entity/EntityUrlTest.php | 4 +- .../PathProcessor/PathProcessorAliasTest.php | 4 +- .../Core/PathProcessor/PathProcessorTest.php | 2 +- .../Tests/Core/Routing/UrlGeneratorTest.php | 11 +- 29 files changed, 157 insertions(+), 185 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 0735dfa3bb2..d24e3cf289b 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -1058,7 +1058,7 @@ function l($text, $path, array $options = array()) { // Add a "data-drupal-link-system-path" attribute to let the // drupal.active-link library know the path in a standardized manner. if (!isset($variables['options']['attributes']['data-drupal-link-system-path'])) { - $variables['options']['attributes']['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($path); + $variables['options']['attributes']['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($path); } } diff --git a/core/includes/theme.inc b/core/includes/theme.inc index 654803cf3b3..423008727de 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1230,7 +1230,7 @@ function template_preprocess_links(&$variables) { // Add a "data-drupal-link-system-path" attribute to let the // drupal.active-link library know the path in a standardized manner. - $li_attributes['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($path); + $li_attributes['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($path); } $item['link'] = $link_element; diff --git a/core/lib/Drupal/Core/CacheDecorator/AliasManagerCacheDecorator.php b/core/lib/Drupal/Core/CacheDecorator/AliasManagerCacheDecorator.php index af48f76d797..8bcbc082ec2 100644 --- a/core/lib/Drupal/Core/CacheDecorator/AliasManagerCacheDecorator.php +++ b/core/lib/Drupal/Core/CacheDecorator/AliasManagerCacheDecorator.php @@ -83,25 +83,25 @@ class AliasManagerCacheDecorator implements CacheDecoratorInterface, AliasManage /** * {@inheritdoc} */ - public function getSystemPath($path, $path_language = NULL) { - $system_path = $this->aliasManager->getSystemPath($path, $path_language); + public function getPathByAlias($alias, $langcode = NULL) { + $path = $this->aliasManager->getPathByAlias($alias, $langcode); // We need to pass on the list of previously cached system paths for this // key to the alias manager for use in subsequent lookups. - $cached = $this->cache->get($system_path); + $cached = $this->cache->get($path); $cached_paths = array(); if ($cached) { $cached_paths = $cached->data; $this->cacheNeedsWriting = FALSE; } $this->preloadPathLookups($cached_paths); - return $system_path; + return $path; } /** * {@inheritdoc} */ - public function getPathAlias($path, $path_language = NULL) { - return $this->aliasManager->getPathAlias($path, $path_language); + public function getAliasByPath($path, $langcode = NULL) { + return $this->aliasManager->getAliasByPath($path, $langcode); } /** diff --git a/core/lib/Drupal/Core/Controller/ExceptionController.php b/core/lib/Drupal/Core/Controller/ExceptionController.php index 4c656aea271..eb3fe6cf2d7 100644 --- a/core/lib/Drupal/Core/Controller/ExceptionController.php +++ b/core/lib/Drupal/Core/Controller/ExceptionController.php @@ -141,7 +141,7 @@ class ExceptionController extends HtmlControllerBase implements ContainerAwareIn watchdog('access denied', $system_path, NULL, WATCHDOG_WARNING); $system_config = $this->container->get('config.factory')->get('system.site'); - $path = $this->container->get('path.alias_manager')->getSystemPath($system_config->get('page.403')); + $path = $this->container->get('path.alias_manager')->getPathByAlias($system_config->get('page.403')); if ($path && $path != $system_path) { if ($request->getMethod() === 'POST') { $subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'POST', array('destination' => $system_path, '_exception_statuscode' => 403) + $request->request->all(), $request->cookies->all(), array(), $request->server->all()); @@ -197,7 +197,7 @@ class ExceptionController extends HtmlControllerBase implements ContainerAwareIn $system_path = $request->attributes->get('_system_path'); - $path = $this->container->get('path.alias_manager')->getSystemPath(\Drupal::config('system.site')->get('page.404')); + $path = $this->container->get('path.alias_manager')->getPathByAlias(\Drupal::config('system.site')->get('page.404')); if ($path && $path != $system_path) { // @todo Um, how do I specify an override URL again? Totally not clear. Do // that and sub-call the kernel rather than using meah(). diff --git a/core/lib/Drupal/Core/Path/AliasManager.php b/core/lib/Drupal/Core/Path/AliasManager.php index 84dbb02f2fd..55d2ddc12cc 100644 --- a/core/lib/Drupal/Core/Path/AliasManager.php +++ b/core/lib/Drupal/Core/Path/AliasManager.php @@ -34,11 +34,11 @@ class AliasManager implements AliasManagerInterface { protected $lookupMap = array(); /** - * Holds an array of path alias for which no source was found. + * Holds an array of aliases for which no path was found. * * @var array */ - protected $noSource = array(); + protected $noPath = array(); /** * Holds the array of whitelisted path aliases. @@ -48,18 +48,18 @@ class AliasManager implements AliasManagerInterface { protected $whitelist; /** - * Holds an array of system paths that have no aliases. + * Holds an array of paths that have no alias. * * @var array */ - protected $noAliases = array(); + protected $noAlias = array(); /** - * Whether lookupPath() has not yet been called. + * Whether preloaded path lookups has already been loaded. * - * @var boolean + * @var array */ - protected $firstLookup = TRUE; + protected $langcodePreloaded = array(); /** * Holds an array of previously looked up paths for the current request path. @@ -88,36 +88,87 @@ class AliasManager implements AliasManagerInterface { } /** - * Implements \Drupal\Core\Path\AliasManagerInterface::getSystemPath(). + * {@inheritdoc} */ - public function getSystemPath($path, $path_language = NULL) { + public function getPathByAlias($alias, $langcode = NULL) { // If no language is explicitly specified we default to the current URL // language. If we used a language different from the one conveyed by the // requested URL, we might end up being unable to check if there is a path // alias matching the URL path. - $path_language = $path_language ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id; - // Lookup the path alias first. - if (!empty($path) && $source = $this->lookupPathSource($path, $path_language)) { - $path = $source; + $langcode = $langcode ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id; + + // If we already know that there are no paths for this alias simply return. + if (empty($alias) || !empty($this->noPath[$langcode][$alias])) { + return $alias; } - return $path; + // Look for the alias within the cached map. + if (isset($this->lookupMap[$langcode]) && ($path = array_search($alias, $this->lookupMap[$langcode]))) { + return $path; + } + + // Look for path in storage. + if ($path = $this->storage->lookupPathSource($alias, $langcode)) { + $this->lookupMap[$langcode][$path] = $alias; + return $path; + } + + // We can't record anything into $this->lookupMap because we didn't find any + // paths for this alias. Thus cache to $this->noPath. + $this->noPath[$langcode][$alias] = TRUE; + return $alias; } /** - * Implements \Drupal\Core\Path\AliasManagerInterface::getPathAlias(). + * {@inheritdoc} */ - public function getPathAlias($path, $path_language = NULL) { + public function getAliasByPath($path, $langcode = NULL) { // If no language is explicitly specified we default to the current URL // language. If we used a language different from the one conveyed by the // requested URL, we might end up being unable to check if there is a path // alias matching the URL path. - $path_language = $path_language ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id; - $result = $path; - if (!empty($path) && $alias = $this->lookupPathAlias($path, $path_language)) { - $result = $alias; + $langcode = $langcode ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id; + + // Check the path whitelist, if the top-level part before the first / + // is not in the list, then there is no need to do anything further, + // it is not in the database. + if (empty($path) || !$this->whitelist->get(strtok($path, '/'))) { + return $path; } - return $result; + + // During the first call to this method per language, load the expected + // paths for the page from cache. + if (empty($this->langcodePreloaded[$langcode])) { + $this->langcodePreloaded[$langcode] = TRUE; + $this->lookupMap[$langcode] = array(); + // Load paths from cache. + if (!empty($this->preloadedPathLookups)) { + $this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups, $langcode); + // Keep a record of paths with no alias to avoid querying twice. + $this->noAlias[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups, array_keys($this->lookupMap[$langcode]))); + } + } + + // If we already know that there are no aliases for this path simply return. + if (!empty($this->noAlias[$langcode][$path])) { + return $path; + } + + // If the alias has already been loaded, return it from static cache. + if (isset($this->lookupMap[$langcode][$path])) { + return $this->lookupMap[$langcode][$path]; + } + + // Try to load alias from storage. + if ($alias = $this->storage->lookupPathAlias($path, $langcode)) { + $this->lookupMap[$langcode][$path] = $alias; + return $alias; + } + + // We can't record anything into $this->lookupMap because we didn't find any + // aliases for this path. Thus cache to $this->noAlias. + $this->noAlias[$langcode][$path] = TRUE; + return $path; } /** @@ -132,9 +183,9 @@ class AliasManager implements AliasManagerInterface { else { $this->lookupMap = array(); } - $this->noSource = array(); - $this->no_aliases = array(); - $this->firstCall = TRUE; + $this->noPath = array(); + $this->noAlias = array(); + $this->langcodePreloaded = array(); $this->preloadedPathLookups = array(); $this->pathAliasWhitelistRebuild($source); } @@ -157,86 +208,6 @@ class AliasManager implements AliasManagerInterface { $this->preloadedPathLookups = $path_list; } - /** - * Given a Drupal system URL return one of its aliases if such a one exists. - * Otherwise, return FALSE. - * - * @param $path - * The path to investigate for corresponding aliases. - * @param $langcode - * Optional language code to search the path with. Defaults to the page language. - * If there's no path defined for that language it will search paths without - * language. - * - * @return - * An aliased path, or FALSE if no path was found. - */ - protected function lookupPathAlias($path, $langcode) { - // During the first call to this method per language, load the expected - // system paths for the page from cache. - if (!empty($this->firstLookup)) { - $this->firstLookup = FALSE; - $this->lookupMap[$langcode] = array(); - // Load system paths from cache. - if (!empty($this->preloadedPathLookups)) { - // Now fetch the aliases corresponding to these system paths. - $this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups, $langcode); - // Keep a record of paths with no alias to avoid querying twice. - $this->noAliases[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups, array_keys($this->lookupMap[$langcode]))); - } - } - // If the alias has already been loaded, return it. - if (isset($this->lookupMap[$langcode][$path])) { - return $this->lookupMap[$langcode][$path]; - } - // Check the path whitelist, if the top-level part before the first / - // is not in the list, then there is no need to do anything further, - // it is not in the database. - elseif (!$this->whitelist->get(strtok($path, '/'))) { - return FALSE; - } - // For system paths which were not cached, query aliases individually. - elseif (!isset($this->noAliases[$langcode][$path])) { - $this->lookupMap[$langcode][$path] = $this->storage->lookupPathAlias($path, $langcode); - return $this->lookupMap[$langcode][$path]; - } - return FALSE; - } - - /** - * Given an alias, return its Drupal system URL if one exists. Otherwise, - * return FALSE. - * - * @param $path - * The path to investigate for corresponding system URLs. - * @param $langcode - * Optional language code to search the path with. Defaults to the page language. - * If there's no path defined for that language it will search paths without - * language. - * - * @return - * A Drupal system path, or FALSE if no path was found. - */ - protected function lookupPathSource($path, $langcode) { - if ($this->whitelist && !isset($this->noSource[$langcode][$path])) { - // Look for the value $path within the cached $map - $source = isset($this->lookupMap[$langcode]) ? array_search($path, $this->lookupMap[$langcode]) : FALSE; - if (!$source) { - if ($source = $this->storage->lookupPathSource($path, $langcode)) { - $this->lookupMap[$langcode][$source] = $path; - } - else { - // We can't record anything into $map because we do not have a valid - // index and there is no need because we have not learned anything - // about any Drupal path. Thus cache to $no_source. - $this->noSource[$langcode][$path] = TRUE; - } - } - return $source; - } - return FALSE; - } - /** * Rebuild the path alias white list. * diff --git a/core/lib/Drupal/Core/Path/AliasManagerInterface.php b/core/lib/Drupal/Core/Path/AliasManagerInterface.php index 10fe4fa60a4..d37db972c07 100644 --- a/core/lib/Drupal/Core/Path/AliasManagerInterface.php +++ b/core/lib/Drupal/Core/Path/AliasManagerInterface.php @@ -10,33 +10,30 @@ namespace Drupal\Core\Path; interface AliasManagerInterface { /** - * Given a path alias, return the internal path it represents. + * Given the alias, return the path it represents. * - * @param $path - * A Drupal path alias. - * @param $path_language + * @param string $alias + * An alias. + * @param string $langcode * An optional language code to look up the path in. * - * @return - * The internal path represented by the alias, or the original alias if no - * internal path was found. + * @return string + * The path represented by alias, or the alias if no path was found. */ - public function getSystemPath($path, $path_language = NULL); + public function getPathByAlias($alias, $langcode = NULL); /** - * Given an internal Drupal path, return the alias set by the administrator. + * Given a path, return the alias. * - * @param $path - * An internal Drupal path. - * - * @param $path_language + * @param string $path + * A path. + * @param string $langcode * An optional language code to look up the path in. * - * @return - * An aliased path if one was found, or the original path if no alias was - * found. + * @return string + * An alias that represents the path, or path if no alias was found. */ - public function getPathAlias($path, $path_language = NULL); + public function getAliasByPath($path, $langcode = NULL); /** * Returns an array of system paths that have been looked up. diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php index 66979d1c699..1c709a11637 100644 --- a/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php +++ b/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php @@ -36,7 +36,7 @@ class PathProcessorAlias implements InboundPathProcessorInterface, OutboundPathP * Implements Drupal\Core\PathProcessor\InboundPathProcessorInterface::processInbound(). */ public function processInbound($path, Request $request) { - $path = $this->aliasManager->getSystemPath($path); + $path = $this->aliasManager->getPathByAlias($path); return $path; } @@ -46,7 +46,7 @@ class PathProcessorAlias implements InboundPathProcessorInterface, OutboundPathP public function processOutbound($path, &$options = array(), Request $request = NULL) { if (empty($options['alias'])) { $langcode = isset($options['language']) ? $options['language']->id : NULL; - $path = $this->aliasManager->getPathAlias($path, $langcode); + $path = $this->aliasManager->getAliasByPath($path, $langcode); } return $path; } diff --git a/core/modules/block/lib/Drupal/block/BlockAccessController.php b/core/modules/block/lib/Drupal/block/BlockAccessController.php index 4f5182b0d47..9e4e472eef0 100644 --- a/core/modules/block/lib/Drupal/block/BlockAccessController.php +++ b/core/modules/block/lib/Drupal/block/BlockAccessController.php @@ -98,7 +98,7 @@ class BlockAccessController extends EntityAccessController implements EntityCont if ($visibility['path']['visibility'] < BLOCK_VISIBILITY_PHP) { // Compare the lowercase path alias (if any) and internal path. $path = current_path(); - $path_alias = Unicode::strtolower($this->aliasManager->getPathAlias($path)); + $path_alias = Unicode::strtolower($this->aliasManager->getAliasByPath($path)); $page_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages)); // When $block->visibility has a value of 0 // (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages diff --git a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php index baf1eb57ce8..6550a0e3938 100644 --- a/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/lib/Drupal/link/Plugin/Field/FieldWidget/LinkWidget.php @@ -210,7 +210,7 @@ class LinkWidget extends WidgetBase { // If internal links are supported, look up whether the given value is // a path alias and store the system path instead. if ($this->supportsInternalLinks() && !UrlHelper::isExternal($value['url'])) { - $parsed_url['path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($parsed_url['path']); + $parsed_url['path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($parsed_url['path']); } $url = Url::createFromPath($parsed_url['path']); diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php index 0a3e64bce54..cae7f4bee44 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocalePathTest.php @@ -109,10 +109,10 @@ class LocalePathTest extends WebTestBase { 'langcode' => Language::LANGCODE_NOT_SPECIFIED, ); $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']); - $lookup_path = $this->container->get('path.alias_manager')->getPathAlias('node/' . $node->id(), 'en'); + $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $node->id(), 'en'); $this->assertEqual($english_path, $lookup_path, 'English language alias has priority.'); // Same check for language 'xx'. - $lookup_path = $this->container->get('path.alias_manager')->getPathAlias('node/' . $node->id(), $prefix); + $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $node->id(), $prefix); $this->assertEqual($custom_language_path, $lookup_path, 'Custom language alias has priority.'); $this->container->get('path.alias_storage')->delete($edit); diff --git a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkForm.php b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkForm.php index b4c6904f22b..c365dae1cc8 100644 --- a/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkForm.php +++ b/core/modules/menu_link/lib/Drupal/menu_link/MenuLinkForm.php @@ -204,7 +204,7 @@ class MenuLinkForm extends EntityForm { public function validate(array $form, array &$form_state) { $menu_link = $this->buildEntity($form, $form_state); - $normal_path = $this->pathAliasManager->getSystemPath($menu_link->link_path); + $normal_path = $this->pathAliasManager->getPathByAlias($menu_link->link_path); if ($menu_link->link_path != $normal_path) { drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $menu_link->link_path, '%normal_path' => $normal_path))); $menu_link->link_path = $normal_path; diff --git a/core/modules/path/lib/Drupal/path/Controller/PathController.php b/core/modules/path/lib/Drupal/path/Controller/PathController.php index 162bf6e87fd..6144b6723d4 100644 --- a/core/modules/path/lib/Drupal/path/Controller/PathController.php +++ b/core/modules/path/lib/Drupal/path/Controller/PathController.php @@ -110,7 +110,7 @@ class PathController extends ControllerBase { // If the system path maps to a different URL alias, highlight this table // row to let the user know of old aliases. - if ($data->alias != $this->aliasManager->getPathAlias($data->source, $data->langcode)) { + if ($data->alias != $this->aliasManager->getAliasByPath($data->source, $data->langcode)) { $row['class'] = array('warning'); } diff --git a/core/modules/path/lib/Drupal/path/Form/PathFormBase.php b/core/modules/path/lib/Drupal/path/Form/PathFormBase.php index 251fbe3d06b..ca507d10479 100644 --- a/core/modules/path/lib/Drupal/path/Form/PathFormBase.php +++ b/core/modules/path/lib/Drupal/path/Form/PathFormBase.php @@ -137,7 +137,7 @@ abstract class PathFormBase extends FormBase { */ public function validateForm(array &$form, array &$form_state) { $source = &$form_state['values']['source']; - $source = $this->aliasManager->getSystemPath($source); + $source = $this->aliasManager->getPathByAlias($source); $alias = $form_state['values']['alias']; // Language is only set if language.module is enabled, otherwise save for all // languages. @@ -160,7 +160,7 @@ abstract class PathFormBase extends FormBase { $pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0; $source = &$form_state['values']['source']; - $source = $this->aliasManager->getSystemPath($source); + $source = $this->aliasManager->getPathByAlias($source); $alias = $form_state['values']['alias']; // Language is only set if language.module is enabled, otherwise save for all // languages. diff --git a/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php b/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php index c2c168b94ea..b4685f7a20e 100644 --- a/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php +++ b/core/modules/path/lib/Drupal/path/Tests/PathLanguageTest.php @@ -177,17 +177,17 @@ class PathLanguageTest extends PathTestBase { // The alias manager has an internal path lookup cache. Check to see that // it has the appropriate contents at this point. $this->container->get('path.alias_manager')->cacheClear(); - $french_node_path = $this->container->get('path.alias_manager')->getSystemPath($french_alias, 'fr'); + $french_node_path = $this->container->get('path.alias_manager')->getPathByAlias($french_alias, 'fr'); $this->assertEqual($french_node_path, 'node/' . $french_node->id(), 'Normal path works.'); // Second call should return the same path. - $french_node_path = $this->container->get('path.alias_manager')->getSystemPath($french_alias, 'fr'); + $french_node_path = $this->container->get('path.alias_manager')->getPathByAlias($french_alias, 'fr'); $this->assertEqual($french_node_path, 'node/' . $french_node->id(), 'Normal path is the same.'); // Confirm that the alias works. - $french_node_alias = $this->container->get('path.alias_manager')->getPathAlias('node/' . $french_node->id(), 'fr'); + $french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $french_node->id(), 'fr'); $this->assertEqual($french_node_alias, $french_alias, 'Alias works.'); // Second call should return the same alias. - $french_node_alias = $this->container->get('path.alias_manager')->getPathAlias('node/' . $french_node->id(), 'fr'); + $french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $french_node->id(), 'fr'); $this->assertEqual($french_node_alias, $french_alias, 'Alias is the same.'); } } diff --git a/core/modules/shortcut/shortcut.module b/core/modules/shortcut/shortcut.module index 870bd8af390..9196fd2fcf5 100644 --- a/core/modules/shortcut/shortcut.module +++ b/core/modules/shortcut/shortcut.module @@ -273,7 +273,7 @@ function shortcut_set_title_exists($title) { */ function shortcut_valid_link($path) { // Do not use URL aliases. - $normal_path = \Drupal::service('path.alias_manager')->getSystemPath($path); + $normal_path = \Drupal::service('path.alias_manager')->getPathByAlias($path); if ($path != $normal_path) { $path = $normal_path; } diff --git a/core/modules/shortcut/src/ShortcutPathValue.php b/core/modules/shortcut/src/ShortcutPathValue.php index 2c8b4e5bf4c..3d88d9b01c6 100644 --- a/core/modules/shortcut/src/ShortcutPathValue.php +++ b/core/modules/shortcut/src/ShortcutPathValue.php @@ -40,7 +40,7 @@ class ShortcutPathValue extends TypedData { */ public function setValue($value, $notify = TRUE) { // Normalize the path in case it is an alias. - $value = \Drupal::service('path.alias_manager')->getSystemPath($value); + $value = \Drupal::service('path.alias_manager')->getPathByAlias($value); if (empty($value)) { $value = ''; } diff --git a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php index 67f40ad28bf..6607b7f732c 100644 --- a/core/modules/shortcut/src/Tests/ShortcutLinksTest.php +++ b/core/modules/shortcut/src/Tests/ShortcutLinksTest.php @@ -62,7 +62,7 @@ class ShortcutLinksTest extends ShortcutTestBase { $this->assertResponse(200); $saved_set = shortcut_set_load($set->id()); $paths = $this->getShortcutInformation($saved_set, 'path'); - $this->assertTrue(in_array($this->container->get('path.alias_manager')->getSystemPath($test['path']), $paths), 'Shortcut created: ' . $test['path']); + $this->assertTrue(in_array($this->container->get('path.alias_manager')->getPathByAlias($test['path']), $paths), 'Shortcut created: ' . $test['path']); $this->assertLink($title, 0, 'Shortcut link found on the page.'); } } diff --git a/core/modules/statistics/statistics.module b/core/modules/statistics/statistics.module index 4b2110cfb12..52152041892 100644 --- a/core/modules/statistics/statistics.module +++ b/core/modules/statistics/statistics.module @@ -169,7 +169,7 @@ function statistics_get($nid) { * A string as a link, truncated to the width, linked to the given $path. */ function _statistics_link($path, $width = 35) { - $title = \Drupal::service('path.alias_manager')->getPathAlias($path); + $title = \Drupal::service('path.alias_manager')->getAliasByPath($path); $title = truncate_utf8($title, $width, FALSE, TRUE); return l($title, $path); } diff --git a/core/modules/system/lib/Drupal/system/Form/SiteInformationForm.php b/core/modules/system/lib/Drupal/system/Form/SiteInformationForm.php index b5d3f5e8a47..01e0d147c93 100644 --- a/core/modules/system/lib/Drupal/system/Form/SiteInformationForm.php +++ b/core/modules/system/lib/Drupal/system/Form/SiteInformationForm.php @@ -94,7 +94,7 @@ class SiteInformationForm extends ConfigFormBase { '#title' => t('Front page'), '#open' => TRUE, ); - $front_page = $site_config->get('page.front') != 'user' ? $this->aliasManager->getPathAlias($site_config->get('page.front')) : ''; + $front_page = $site_config->get('page.front') != 'user' ? $this->aliasManager->getAliasByPath($site_config->get('page.front')) : ''; $form['front_page']['site_frontpage'] = array( '#type' => 'textfield', '#title' => t('Default front page'), @@ -139,7 +139,7 @@ class SiteInformationForm extends ConfigFormBase { } else { // Get the normal path of the front page. - form_set_value($form['front_page']['site_frontpage'], $this->aliasManager->getSystemPath($form_state['values']['site_frontpage']), $form_state); + form_set_value($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state['values']['site_frontpage']), $form_state); } // Validate front page path. if (!drupal_valid_path($form_state['values']['site_frontpage'])) { @@ -147,10 +147,10 @@ class SiteInformationForm extends ConfigFormBase { } // Get the normal paths of both error pages. if (!empty($form_state['values']['site_403'])) { - form_set_value($form['error_page']['site_403'], $this->aliasManager->getSystemPath($form_state['values']['site_403']), $form_state); + form_set_value($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state['values']['site_403']), $form_state); } if (!empty($form_state['values']['site_404'])) { - form_set_value($form['error_page']['site_404'], $this->aliasManager->getSystemPath($form_state['values']['site_404']), $form_state); + form_set_value($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state['values']['site_404']), $form_state); } // Validate 403 error path. if (!empty($form_state['values']['site_403']) && !drupal_valid_path($form_state['values']['site_403'])) { diff --git a/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php b/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php index 07bf0061684..0baae455b82 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php @@ -95,8 +95,8 @@ class AliasTest extends PathUnitTestBase { ); $aliasStorage->save($path['source'], $path['alias']); - $this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'Basic alias lookup works.'); - $this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'Basic source lookup works.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Basic alias lookup works.'); + $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Basic source lookup works.'); // Create a language specific alias for the default language (English). $path = array( @@ -107,8 +107,8 @@ class AliasTest extends PathUnitTestBase { $aliasStorage->save($path['source'], $path['alias'], $path['langcode']); // Hook that clears cache is not executed with unit tests. \Drupal::service('path.alias_manager.cached')->cacheClear(); - $this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'English alias overrides language-neutral alias.'); - $this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'English source overrides language-neutral source.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'English alias overrides language-neutral alias.'); + $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'English source overrides language-neutral source.'); // Create a language-neutral alias for the same path, again. $path = array( @@ -116,7 +116,7 @@ class AliasTest extends PathUnitTestBase { 'alias' => 'bar', ); $aliasStorage->save($path['source'], $path['alias']); - $this->assertEqual($aliasManager->getPathAlias($path['source']), "users/Dries", 'English alias still returned after entering a language-neutral alias.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), "users/Dries", 'English alias still returned after entering a language-neutral alias.'); // Create a language-specific (xx-lolspeak) alias for the same path. $path = array( @@ -125,9 +125,9 @@ class AliasTest extends PathUnitTestBase { 'langcode' => 'xx-lolspeak', ); $aliasStorage->save($path['source'], $path['alias'], $path['langcode']); - $this->assertEqual($aliasManager->getPathAlias($path['source']), "users/Dries", 'English alias still returned after entering a LOLspeak alias.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), "users/Dries", 'English alias still returned after entering a LOLspeak alias.'); // The LOLspeak alias should be returned if we really want LOLspeak. - $this->assertEqual($aliasManager->getPathAlias($path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.'); // Create a new alias for this path in English, which should override the // previous alias for "user/1". @@ -139,22 +139,22 @@ class AliasTest extends PathUnitTestBase { $aliasStorage->save($path['source'], $path['alias'], $path['langcode']); // Hook that clears cache is not executed with unit tests. $aliasManager->cacheClear(); - $this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'Recently created English alias returned.'); - $this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'Recently created English source returned.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Recently created English alias returned.'); + $this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Recently created English source returned.'); // Remove the English aliases, which should cause a fallback to the most // recently created language-neutral alias, 'bar'. $aliasStorage->delete(array('langcode' => 'en')); // Hook that clears cache is not executed with unit tests. $aliasManager->cacheClear(); - $this->assertEqual($aliasManager->getPathAlias($path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.'); + $this->assertEqual($aliasManager->getAliasByPath($path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.'); // Test the situation where the alias and language are the same, but // the source differs. The newer alias record should be returned. $aliasStorage->save('user/2', 'bar'); // Hook that clears cache is not executed with unit tests. $aliasManager->cacheClear(); - $this->assertEqual($aliasManager->getSystemPath('bar'), 'user/2', 'Newer alias record is returned when comparing two Language::LANGCODE_NOT_SPECIFIED paths with the same alias.'); + $this->assertEqual($aliasManager->getPathByAlias('bar'), 'user/2', 'Newer alias record is returned when comparing two Language::LANGCODE_NOT_SPECIFIED paths with the same alias.'); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Path/UrlAlterFunctionalTest.php b/core/modules/system/lib/Drupal/system/Tests/Path/UrlAlterFunctionalTest.php index 5772e0aff03..49244084db6 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Path/UrlAlterFunctionalTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Path/UrlAlterFunctionalTest.php @@ -119,7 +119,7 @@ class UrlAlterFunctionalTest extends WebTestBase { */ protected function assertUrlInboundAlter($original, $final) { // Test inbound altering. - $result = $this->container->get('path.alias_manager')->getSystemPath($original); + $result = $this->container->get('path.alias_manager')->getPathByAlias($original); $this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result))); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/MockAliasManager.php b/core/modules/system/lib/Drupal/system/Tests/Routing/MockAliasManager.php index 3d8d737da96..53370999ae9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/MockAliasManager.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/MockAliasManager.php @@ -62,18 +62,21 @@ class MockAliasManager implements AliasManagerInterface { /** * {@inheritdoc} */ - public function getSystemPath($path, $path_language = NULL) { - $language = $path_language ?: $this->defaultLanguage; - return $this->systemPaths[$path][$language]; + public function getPathByAlias($alias, $langcode = NULL) { + $langcode = $langcode ?: $this->defaultLanguage; + return $this->systemPaths[$alias][$langcode]; } /** * {@inheritdoc} + * @param $path + * @param null $langcode + * @return */ - public function getPathAlias($path, $path_language = NULL) { - $language = $path_language ?: $this->defaultLanguage; + public function getAliasByPath($path, $langcode = NULL) { + $langcode = $langcode ?: $this->defaultLanguage; $this->lookedUp[$path] = 1; - return $this->aliases[$path][$language]; + return $this->aliases[$path][$langcode]; } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/argument_default/Raw.php b/core/modules/views/lib/Drupal/views/Plugin/views/argument_default/Raw.php index 5f8a3495740..3f144aee30b 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/argument_default/Raw.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/argument_default/Raw.php @@ -101,7 +101,7 @@ class Raw extends ArgumentDefaultPluginBase { public function getArgument() { $path = $this->request->attributes->get('_system_path'); if ($this->options['use_alias']) { - $path = $this->aliasManager->getPathAlias($path); + $path = $this->aliasManager->getAliasByPath($path); } $args = explode('/', $path); if (isset($args[$this->options['index']])) { diff --git a/core/modules/views/tests/Drupal/views/Tests/Plugin/argument_default/RawTest.php b/core/modules/views/tests/Drupal/views/Tests/Plugin/argument_default/RawTest.php index af285f9959e..397cb3602ca 100644 --- a/core/modules/views/tests/Drupal/views/Tests/Plugin/argument_default/RawTest.php +++ b/core/modules/views/tests/Drupal/views/Tests/Plugin/argument_default/RawTest.php @@ -42,7 +42,7 @@ class RawTest extends UnitTestCase { $request = new Request(array(), array(), array('_system_path' => 'test/example')); $alias_manager = $this->getMock('Drupal\Core\Path\AliasManagerInterface'); $alias_manager->expects($this->never()) - ->method('getPathAlias'); + ->method('getAliasByPath'); // Don't use aliases. $raw = new Raw(array(), 'raw', array(), $request, $alias_manager); @@ -64,7 +64,7 @@ class RawTest extends UnitTestCase { // Setup an alias manager with a path alias. $alias_manager = $this->getMock('Drupal\Core\Path\AliasManagerInterface'); $alias_manager->expects($this->any()) - ->method('getPathAlias') + ->method('getAliasByPath') ->with($this->equalTo('test/example')) ->will($this->returnValue('other/example')); diff --git a/core/modules/views_ui/views_ui.module b/core/modules/views_ui/views_ui.module index b1c3437f964..a8cdfd56199 100644 --- a/core/modules/views_ui/views_ui.module +++ b/core/modules/views_ui/views_ui.module @@ -320,7 +320,7 @@ function views_ui_views_analyze(ViewExecutable $view) { continue; } if ($display->hasPath() && $path = $display->getOption('path')) { - $normal_path = \Drupal::service('path.alias_manager.cached')->getSystemPath($path); + $normal_path = \Drupal::service('path.alias_manager.cached')->getPathByAlias($path); if ($path != $normal_path) { $ret[] = Analyzer::formatMessage(t('You have configured display %display with a path which is an path alias as well. This might lead to unwanted effects so better use an internal path.', array('%display' => $display->display['display_title'])), 'warning'); } diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php index c25be2091a6..2ff0bf796b6 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityUrlTest.php @@ -251,9 +251,9 @@ class EntityUrlTest extends UnitTestCase { } /** - * Tests the getSystemPath() method. + * Tests the getPathByAlias() method. * - * @covers ::getSystemPath() + * @covers ::getPathByAlias() */ public function testGetSystemPath() { $entity_type = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); diff --git a/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php index 40c8de0aaf4..d810d1c7e5b 100644 --- a/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php +++ b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php @@ -54,7 +54,7 @@ class PathProcessorAliasTest extends UnitTestCase { */ public function testProcessInbound() { $this->aliasManager->expects($this->exactly(2)) - ->method('getSystemPath') + ->method('getPathByAlias') ->will($this->returnValueMap(array( array('urlalias', NULL, 'internal-url'), array('url', NULL, 'url'), @@ -73,7 +73,7 @@ class PathProcessorAliasTest extends UnitTestCase { */ public function testProcessOutbound() { $this->aliasManager->expects($this->exactly(2)) - ->method('getPathAlias') + ->method('getAliasByPath') ->will($this->returnValueMap(array( array('internal-url', NULL, 'urlalias'), array('url', NULL, 'url'), diff --git a/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php index 8f877f7f488..f58edc55006 100644 --- a/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php +++ b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorTest.php @@ -110,7 +110,7 @@ class PathProcessorTest extends UnitTestCase { ); $alias_manager->expects($this->any()) - ->method('getSystemPath') + ->method('getPathByAlias') ->will($this->returnValueMap($system_path_map)); // Create a stub config factory with all config settings that will be checked diff --git a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php index 151b74907db..5226261e431 100644 --- a/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/UrlGeneratorTest.php @@ -115,7 +115,7 @@ class UrlGeneratorTest extends UnitTestCase { ->getMock(); $alias_manager->expects($this->any()) - ->method('getPathAlias') + ->method('getAliasByPath') ->will($this->returnCallback(array($this, 'aliasManagerCallback'))); $this->aliasManager = $alias_manager; @@ -144,11 +144,12 @@ class UrlGeneratorTest extends UnitTestCase { } /** - * Return value callback for the getPathAlias() method on the mock alias manager. + * Return value callback for the getAliasByPath() method on the mock alias + * manager. * - * Ensures that by default the call to getPathAlias() will return the first argument - * that was passed in. We special-case the paths for which we wish it to return an - * actual alias. + * Ensures that by default the call to getAliasByPath() will return the first + * argument that was passed in. We special-case the paths for which we wish it + * to return an actual alias. * * @return string */