Problem/Motivation

Some work is getting done in core to support getting fallbacks for path aliases here (patch from this issue is needed) : #3091336: Allow altering of the language fallback for path aliases
The similar module language_hierarchy is already supporting this #2830291: D8 URL alias language fallbacks
This is indeed useful and needed for us.

Proposed resolution

I don't really know if that's in scope for the module, or even if we should stop the duplicated work as was discussed in #2989537: Standardize language-fallback modules on entity_language_fallback
But anyway, we need this and we use this module, so we will probably implement something.

Comments

Radelson created an issue. See original summary.

radelson’s picture

Status: Active » Needs review
StatusFileSize
new1.29 KB

Here is a small patch.

I think even though the module is called ENTITY_language_fallback, getting the path aliases for these entities is probably in scope. Internally we will probably use a patched version supporting all the operations (even operations not directly related to entities), I think it's pretty safe to simply default to getting the fallback chain like this but then it would apply not only to entities.

  /* @var $fallback_controller \Drupal\entity_language_fallback\FallbackController */
  $fallback_controller = \Drupal::service('language_fallback.controller');

  if ($operation == 'entity_upcast' || $operation == 'entity_view') {
    if ($new_candidates = $fallback_controller->getEntityFallbackCandidates($context['data'], $context['langcode'])) {
      $candidates = $new_candidates;
    }
  } else {
    if ($new_candidates = $fallback_controller->getFallbackChain($context['langcode'])) {
      $candidates = $new_candidates;
    }
  }
radelson’s picture

Assigned: radelson » Unassigned
radelson’s picture

Eh, probably should have thought about it a little more. It's probably not safe at all to default to getting the fallback chain.

Also, should have spent more time reading the patches from core and language_hierarchy. As is, it introduces bugs as the paths returned are not properly ordered following the fallback chain.

We need to reorder them by altering the query like in this patch from language_hierarchy : https://www.drupal.org/project/language_hierarchy/issues/2830291#comment...

We don't have a table to join on like in language_hierarchy but there is a "generic" implementation used for testing the core patch that should work for us too without changing anything :

/**
 * Implements hook_query_TAG_alter().
 */
function path_alias_language_fallback_test_query_path_alias_language_fallback_alter(AlterableInterface $query) {
  /** @var \Drupal\Core\Database\Query\SelectInterface $query */
  // Replace the existing language code ordering.
  $fields = &$query->getOrderBy();
  unset($fields['base_table.langcode']);

  // Sort results according to the order of the language fallback chain, which
  // can be found in the values of the langcode condition.
  $conditions = &$query->conditions();
  foreach ($conditions as $condition) {
    if (is_array($condition) && isset($condition['field']) && $condition['field'] === 'base_table.langcode') {
      $order = '(CASE ';
      foreach (array_values($condition['value']) as $index => $lang) {
        $order .= "WHEN base_table.langcode = '$lang' THEN $index ";
      }
      $order .= 'END)';
      $order_alias = $query->addExpression($order, 'order');
      $fields = [$order_alias => 'ASC'] + $fields;
      break;
    }
  }
}

Probably not terribly good performance wise but anyway.

Here is a patch with the hook added.

radelson’s picture

Disregard last patch from #4, here is another one.
Sorry.

dmitry.korhov’s picture

Status: Needs review » Needs work

Tested and patch does not work.
STR:
1) Add Dutch language
2) Set English as fallback
3) Add alias to English node (/en-alias)
4) Open /nl/en-alias
AR: 404 Not found
ER: English node is opened

dmitry.korhov’s picture

Status: Needs work » Needs review
radelson’s picture

Issue summary: View changes
radelson’s picture

Yeah, sorry, should have been more clear about this, I updated the summary.

Also, we found some flaws with the previous patch :
- The initial langcode needs to be put in the first position, it's done in the function getEntityFallbackCandidates but it's not done for getFallbackChain => probably it'd be cleaner if not duplicated for the two function at two places

- The Undefined language should be added to the chain in last position for the path aliases (see the function addLanguageFallback from core patch)

anna d’s picture

Doesn't work #9 for me.

Drupal 9.5
Entity Language Fallback: 1.x-dev || 1.4

briantschu’s picture

I updated the patch from #9 to be compatible with version 1.5.0, which matches the latest 1.x-dev.

alberto56’s picture

Here is a version of the patch which applies to the latest dev version

baikho’s picture

baikho’s picture

Issue summary: View changes