Problem/Motivation

I have a mutlilingual website with many content type are translated.

For my articles, I have a reference field to a taxonomy term (blog, news, etc.) that are translated but the field is common at all the article translations (so we can't have "News" in french and "Blog" in english per example).

The path pattern is [node:field_article_type]/[node:title]

When I create or edit a content "article", the path is translated, but when I generate in bulk from the module settings, the path are regenerate without the translation.

So, when I create or edit the content, I have :

- FRENCH : /actualite/le-titre-du-noeud
- ENGLISH : /news/the-node-title

When I bulk update

- FRENCH : /news/le-titre-du-noeud
- ENGLISH : /news/the-node-title

The node title is translated, but not the field "field_article_type".

Steps to reproduce

- Create a multilingual content type + a taxonomy with many terms translated
- Add a taxonomy reference field in the content type
- Create a pattern with the taxonomy reference field inside
- Try to create and/or edit a content
- Try to generate the path in bulk.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork token-3281871

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Bessonweb created an issue. See original summary.

bessonweb’s picture

Issue summary: View changes
chewi3’s picture

I had the same issue with a project I was working on. The problem seems to occur during token replacement (token.tokens.inc around L1750). I think this is more of a token issue, since the langcode seems to be sent correctly from pathauto. Unfortunately during the process of using field rendering we lose the language context and default to the current language (which in these cases is whatever language you are in when you start the bulk generation).

For anyone that has the same issue and needs a quick fix, I ended up doing a token patch like this before the "$field_name->view" line (to replace the field rendering logic):

if ($entity->getFieldDefinition($field_name)->get('field_type') == 'entity_reference') {
  $referencedEntity = $entity->get($field_name)->referencedEntities()[0];
  if ($referencedEntity) {
    $referencedEntity = \Drupal::service('entity.repository')->getTranslationFromContext($referencedEntity, $langcode);
    $referencedEntityType = $referencedEntity->getEntityTypeId();
    $replacements[$original] = Link::fromTextAndUrl($referencedEntity->label(),
      Url::fromRoute("entity.$referencedEntityType.canonical", [$referencedEntityType => $referencedEntity->id()])
     )->toString();
  }
  continue;
}

This is obviously not a great fix, but was enough to solve the issue for me and might help someone else (though it probably depends on which tokens you use in alias patterns, I only had taxonomy term references).

nils.destoop’s picture

Title: Bulk generate don't translate the path » Token generation of entity references are not respecting the given langcode
Project: Pathauto » Token

Moving this to token module + updating title

kdbrums’s picture

Hi,

same issue for me. The fix of Chewi3 #3 works fine to solve my problem.

Thx

dimas11’s picture

#3 Is really works. Here is a patch.

rohit.rawat619’s picture

Assigned: Unassigned » rohit.rawat619
rohit.rawat619’s picture

Assigned: rohit.rawat619 » Unassigned
Status: Active » Needs review
StatusFileSize
new54.69 KB
dimas11’s picture

StatusFileSize
new1.72 KB

Added condition to fix an error on media remote_video entities

dimas11’s picture

We noticed that there was an error with Token and Hook event dispatcher module after using #6 patch with core 10.2.1. We reported it originally as an issue to Hook event dispatcher (#3414662), but then debugged it more and noticed that this patch in Token module was causing the following error when running cron or saving media of type remote_video:

Error: Call to undefined method Drupal\Core\Field\BaseFieldDefinition::get() in _field_tokens() (line 1790 of XXX/web/modules/contrib/token/token.tokens.inc)

berdir’s picture

Status: Needs review » Needs work

There's no need for such a long comment, and it should not use get(), it should use getType(), which is a method on FieldDefinitionInterface.

pradhumanjain2311 made their first commit to this issue’s fork.

d.steindl made their first commit to this issue’s fork.

d.steindl’s picture

Version: 8.x-1.10 » 8.x-1.x-dev

- Rebased the issue fork to the latest 8.x-1.x
- Moved the changes from token.tokens.inc to TokenTokensHooks
- Removed the unnecessary check `method_exists($field_definition, 'get')` since its not used anyways
- Re-use the already existing variable `$field_definition` for the field-type == "entity_reference" condition

I did that because I need the change in one of my projects, but i still want to make sure that everyone here is aware that this patch changes the behavior of entity reference tokens completely.
Without the changes, the field formatter for the display mode "token" was used to render the field.
With the MR changes applied, there is no way to control how entity_reference fields get rendered. It just takes the first referenced entity and returns a link to the translated entity (by using it's label + canonical URL) for the token replacement. That means we've lost multivalue field support & the possibility to change configuration.

I don't think that this is a change which should get merged.
For a sustainable fix, someone needs to think about how to pass the proper language context to the field renderer instead.