I have a multilingual site, where Finnish is the default language. I have node types where I've enabled translations and Pathauto works wonderfully there. Now I'm trying to create a new node type in which I have unchecked the "Enable translation" button, ie. I want to show the same node on all languages. If I create (or later change to, doesn't seem to affect this) the node as language undefined, Pathauto works again wonderfully and creates the url aliases as expected (currently using "my-url/[node:title]") for all languages. So "my-url/test" goes to Finnish, "en/my-url/test" goes to English and so on.

Problem is that if I create or change the node's language to anything else, Pathauto stops working. If I select Finnish, for example, "my-url/test" works but other language urls are in format "en/node/XX". If I make the node language English, "en/my-url/test" works but other languages, including Finnish, use "de/node/XX" (or just "node/XX" for Finnish).

Comments

teemuaro created an issue. See original summary.

bohus ulrych’s picture

Hi,
is there any solution for this?
In my case I have English version with alias, but French translation doesn't exists. Still I want to display FR aliased URL, instead of bad looking /fr/node/nid
Would it be possible to
1) use some fallback to existing English alias, e.g. fr/en-title
2) or somehow create aliases also for non existing languages/translations?
Thanks

marcoka’s picture

I have that somehow too for taxonomy terms now. Also for some nodes.
Aliases are generated but do not work because the alias "click edit"
Has no language set when generated by mass generation.
If i set it manually it works.

Btw. the vocabulary is set to "german" on the vocabulary settings.

arla’s picture

My workaround looks like this:

function MYMODULE_entity_insert(EntityInterface $entity) {
  _MYMODULE_update_untranslated_path($entity, 'insert');
}

function MYMODULE_entity_update(EntityInterface $entity) {
  _MYMODULE_update_untranslated_path($entity, 'update');
}

/**
 * Trigger Pathauto for untranslated content.
 *
 * Pathauto itself manages the alias for the active language only.
 */
function _MYMODULE_update_untranslated_path(EntityInterface $entity, $mode = 'update') {
  /** @var \Drupal\pathauto\PathautoGeneratorInterface $pathauto_generator */
  $pathauto_generator = \Drupal::service('pathauto.generator');

  // Only Pathauto-enabled, translatable entities are welcome here.
  if (!$pathauto_generator->getPatternByEntity($entity) || !($entity instanceof TranslatableInterface)) {
    return;
  }

  $translated_languages = $entity->getTranslationLanguages();
  $all_languages = \Drupal::languageManager()->getLanguages();
  $untranslated_languages = array_diff_key($all_languages, $translated_languages);
  foreach ($untranslated_languages as $langcode => $language) {
    // Create a mock translation (not saved) just to make the generator use the desired language.
    $translation = $entity->addTranslation($langcode, $entity->toArray());
    $pathauto_generator->updateEntityAlias($translation, $mode);
  }
}
geek-merlin’s picture

Status: Active » Closed (duplicate)