You are here

public function XmlSitemapGenerator::getPathAlias in XML sitemap 8

Same name and namespace in other branches
  1. 2.x src/XmlSitemapGenerator.php \Drupal\xmlsitemap\XmlSitemapGenerator::getPathAlias()

Given an internal Drupal path, return the alias for the path.

This is similar to drupal_get_path_alias(), but designed to fetch all aliases at once so that only one database query is executed instead of severa or possibly thousands during sitemap generation.

Parameters

string $path: An internal Drupal path.

string $language: A language code to use when looking up the paths.

Overrides XmlSitemapGeneratorInterface::getPathAlias

File

src/XmlSitemapGenerator.php, line 163

Class

XmlSitemapGenerator
XmlSitemap generator service class.

Namespace

Drupal\xmlsitemap

Code

public function getPathAlias($path, $language) {
  $query = $this->connection
    ->select('path_alias', 'u');
  $query
    ->fields('u', [
    'path',
    'alias',
  ]);
  if (!isset(static::$aliases)) {
    $query
      ->condition('langcode', LanguageInterface::LANGCODE_NOT_SPECIFIED, '=');
    static::$aliases[LanguageInterface::LANGCODE_NOT_SPECIFIED] = $query
      ->execute()
      ->fetchAllKeyed();
  }
  if ($language !== LanguageInterface::LANGCODE_NOT_SPECIFIED && static::$lastLanguage != $language) {
    unset(static::$aliases[static::$lastLanguage]);
    $query
      ->condition('langcode', $language, '=');
    $query
      ->orderBy('id');
    static::$aliases[$language] = $query
      ->execute()
      ->fetchAllKeyed();
    static::$lastLanguage = $language;
  }
  if ($language !== LanguageInterface::LANGCODE_NOT_SPECIFIED && isset(static::$aliases[$language][$path])) {
    return static::$aliases[$language][$path];
  }
  elseif (isset(static::$aliases[LanguageInterface::LANGCODE_NOT_SPECIFIED][$path])) {
    return static::$aliases[LanguageInterface::LANGCODE_NOT_SPECIFIED][$path];
  }
  else {
    return $path;
  }
}