/** * {@inheritdoc} */ 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. $langcode = $langcode ?: $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL)->getId(); // If we already know that there are no paths for this alias simply return. if (empty($alias) || !empty($this->noPath[$langcode][$alias])) { return $alias; } // 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; $this->cacheNeedsWriting = TRUE; 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; }
protected function lookup($type, $lookup, $langcode = null) { if (self::SOURCE !== $type) { // Exclude all admin paths if ($this->excludeAdminPath && path_is_admin($lookup)) { return $lookup; } // Also check for whitelist if (false !== $this->whitelist && !isset($this->whitelist[strtok($lookup, '/')])) { return $lookup; } } if (!$langcode) { $langcode = $GLOBALS['language']->getId(); } // Compute cache key lookup if (self::SOURCE === $type) { $dest = self::ALIAS; } else { $type = self::ALIAS; $dest = self::SOURCE; if ($this->doCache && !$this->isPreloaded) { if (empty($this->data[self::ALIAS][$langcode])) { $this->data[self::ALIAS][$langcode] = []; } $this->data[self::ALIAS][$langcode] += $this->preloadSourceItems($langcode); $this->isPreloaded = true; } } if (isset($this->data[$type][$langcode][$lookup])) { $found = $this->data[$type][$langcode][$lookup]; return $found ? $found : $lookup; } $this->dataIsUpdated = true; if (self::SOURCE === $type) { $found = $this->storage->lookupPathSource($lookup, $langcode); } else { $found = $this->storage->lookupPathAlias($lookup, $langcode); } if ($found) { $this->data[$type][$langcode][$lookup] = $found; $this->data[$dest][$langcode][$found] = $lookup; } else { $this->data[$type][$langcode][$lookup] = false; } return $found ? $found : $lookup; }
public function testTheMockUp() { // Tests registered aliases $this->assertSame('alias-en', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'en')); $this->assertSame('alias-fr', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'fr')); $this->assertSame('alias-und', $this->aliasStorage->lookupPathAlias('duplicate-langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED)); // Non existing language returns the undefined language alias $this->assertSame('alias-und', $this->aliasStorage->lookupPathAlias('duplicate-langcode', 'martian')); // More basic tests $this->assertSame('duplicate-langcode', $this->aliasStorage->lookupPathSource('alias-fr', 'fr')); $this->assertSame('duplicate-langcode', $this->aliasStorage->lookupPathSource('alias-en', 'en')); $this->assertFalse($this->aliasStorage->lookupPathSource('alias-fr', LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->assertFalse($this->aliasStorage->lookupPathSource('alias-en', LanguageInterface::LANGCODE_NOT_SPECIFIED)); $this->assertFalse($this->aliasStorage->lookupPathSource('alias-fr', 'en')); $this->assertFalse($this->aliasStorage->lookupPathSource('alias-en', 'fr')); }