LH limits to 1 alias when preloading multiple aliases

Created on 11 May 2020, over 4 years ago
Updated 29 March 2024, 8 months ago

As reported in #3098399-2: Path alias fallbacks follow-up: tests & profiling :

The query limit in language_hierarchy_query_path_alias_language_fallback_alter() which was introduced at #2830291: D8 URL alias language fallbacks .

We found this while using the layout builder and placing custom blocks with links. All links except one are un-aliased.

The AliasManager::getAliasByPath loads all paths (preloadedPathLookups) and fetch their aliases via Drupal\Core\Path\AliasRepository::preloadPathAlias. In this method multiple paths could be added to the query. LH limits this to only one result which leads to have only one link left with an aliased path.

Example query from AliasRepository::preloadPathAlias (Limit is added by LH):

SELECT base_table.path AS path, base_table.alias AS alias
FROM
path_alias base_table
LEFT OUTER JOIN language_hierarchy_priority lhp ON base_table.langcode = lhp.langcode
WHERE (base_table.status = 1) AND ((base_table.path LIKE '/node/81' ESCAPE '\\') OR (base_table.path LIKE '/node/73' ESCAPE '\\')) AND (base_table.langcode IN ('de', 'und'))
ORDER BY lhp.priority DESC, base_table.id ASC
LIMIT 1 OFFSET 0

What happens if we remove the limit?

Hook from language_hierarchy.module:

/**
 * Implements hook_query_TAG_alter().
 *
 * Order the fallback candidates to be used when querying path aliases.
 *
 * @see \Drupal\path_alias\AliasRepository::addLanguageFallback()
 */
function language_hierarchy_query_path_alias_language_fallback_alter(AlterableInterface $query) {
  if (!$query instanceof SelectInterface) {
    return;
  }

  $alias = $query->leftJoin('language_hierarchy_priority', 'lhp', "base_table.langcode = %alias.langcode");
  // Replace the existing language code ordering.
  $fields = &$query->getOrderBy();
  unset($fields['base_table.langcode']);
  $fields = [$alias . '.priority' => 'DESC'] + $fields;
  // Limit the query as only the first result will be used anyway.
  $query->range(0, 1);   <---------------------------------
}
🐛 Bug report
Status

Fixed

Version

2.0

Component

Code

Created by

🇬🇧United Kingdom james.williams

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024