Problem/Motivation
Following is the current language fallback logic used in the path.alias_repository service:
// Always get the language-specific alias before the language-neutral one.
// For example 'de' is less than 'und' so the order needs to be ASC, while
// 'xx-lolspeak' is more than 'und' so the order needs to be DESC.
$langcode_list = [$langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED];
This logic doesn't take into account where custom/contrib modules may implement hook_language_fallback_candidates_alter()
to add/remove fallback candidate languages.
Example of one such contrib module:
Language hierarchy: https://www.drupal.org/project/language_hierarchy
Steps to reproduce
* Configure at least two languages - let's say Spanish and English.
* Implement hook_language_fallback_candidates_alter()
, perhaps using a module like language_hierarchy, to set a specific ordered set of language fallbacks - let's go with 1. Spanish, 2. English
* Create a valid English alias that is globally unique.
* Visit that alias whilst viewing the site in Spanish.
* Current behaviour is that the alias isn't recognised, and the page would be a 404. Behaviour should be (due to what hook_language_fallback_candidates_alter()
should/could make possible) that the English alias is used, so page that the underlying system route which that alias is for, is shown to the user.
Proposed resolution
Ensure the alias repository invokes hook_language_fallback_candidates_alter()
when appropriate, whilst preserving existing behaviour for sites that do not use it (i.e. only support falling back to aliases using LanguageInterface::LANGCODE_NOT_SPECIFIED
).
More details of subtleties in comments 26, 67, etc.
Remaining tasks
Complete review. Merge MR !5373, which is for the 11.x branch. MR !5332 is for the 10.2.x branch.
User interface changes
None
API changes
As in the change record, core/modules/path_alias/src/AliasRepository.php has a new constructor item - its use is optional for now, but could then be made required for the next major Drupal core version.
Data model changes
None
Release notes snippet
NA
Original post
Following is the current language fallback logic used in the path.alias_repository service:
// Always get the language-specific alias before the language-neutral one.
// For example 'de' is less than 'und' so the order needs to be ASC, while
// 'xx-lolspeak' is more than 'und' so the order needs to be DESC.
$langcode_list = [$langcode, LanguageInterface::LANGCODE_NOT_SPECIFIED];
This logic doesn't take into account where custom/contrib modules may implement hook_language_fallback_candidates_alter()
to add/remove fallback candidate languages.
Example of one such contrib module:
Language hierarchy: https://www.drupal.org/project/language_hierarchy
Attaching a patch to user proper fallback mechanism. i.e.:
// Get list of all languages that current language falls back on.
$langcode_list = $this->languageManager->getFallbackCandidates([
'langcode' => $langcode,
'operation' => 'path_alias',
]);