Token for entity reference returns default language instead of translation

Created on 3 September 2017, over 7 years ago
Updated 8 August 2023, over 1 year ago

There are a great many issues that already documented this, including one that supposedly fixed the issue. However, I am still seeing the issue. Here is my current setup:
Drupal 8.3.7
Ctools 8.x-3.0
Pathauto 8.x-1.0
Token 8.x-1.0

All of the above are up to date.

I have the default language (English) and one additional language (Spanish) enabled.
I have a Book page that references a custom content type called Product via a field with machine name field_product. Both the Book page content type and the Product content type are translatable.
I have two Pathauto patterns, one for each language on my site, that look like literaltext/[node:field_product]/[node:title] (for the default site language) and translatedliteraltext/[node:field_product]/[node:title] (for the translated language).
Assume the Book page has a node with title "BookPage" and translated title "BookPageTranslated".
Assume that the Product has a node with title "Product" and translated title "ProductTranslated".

If admin/structure/types/manage/book/fields/node.book.field_product (field_product in the Book page) has "Users may translate this field" unchecked then the alias I get for my translated Book page looks like translatedliteraltext/product/bookpagetranslated.
If admin/structure/types/manage/book/fields/node.book.field_product (field_product in the Book page) has "Users may translate this field" checked then the alias I get for my translated Book page looks like tranlsatedliteraltext/producttranslated/bookpagetranslated.

The expected outcome is that I will get the translated title for the referenced entity token (in this case [node:field_product]) regardless of whether the entity reference field (in this case field_product) in the entity that does the referencing (in this case the Book page) indicates that its entity reference field allows users to translate it.

๐Ÿ› Bug report
Status

RTBC

Version

1.0

Component

Code

Created by

๐Ÿ‡บ๐Ÿ‡ธUnited States vegantriathlete Lakewood, CO

Live updates comments and jobs are added and updated live.
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.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    RTBC

    We've been using this patch for years, the patch 21 is the best one yet and I approve! Fixes an issue that was present in a previous version of the patch. I recommend going forward with patch #21 asap.

  • Status changed to Needs work about 1 year ago
  • ๐Ÿ‡จ๐Ÿ‡ญSwitzerland berdir Switzerland
    +++ b/token.tokens.inc
    @@ -1227,6 +1227,9 @@ function book_tokens($type, $tokens, array $data, array $options, BubbleableMeta
             $child_node = Node::load($book['nid']);
    +        if (!empty($options['langcode']) && $child_node->hasTranslation($options['langcode'])) {
    +          $child_node = \Drupal::service('entity.repository')->getTranslationFromContext($child_node, $options['langcode']);
    +        }
    
    @@ -1237,42 +1240,52 @@ function book_tokens($type, $tokens, array $data, array $options, BubbleableMeta
    +      if (isset($book_node)) {
    +        if (!empty($options['langcode']) && $book_node->hasTranslation($options['langcode'])) {
    +          $book_node = \Drupal::service('entity.repository')->getTranslationFromContext($book_node, $options['langcode']);
    +        }
    +        foreach ($tokens as $name => $original) {
    +          switch ($name) {
    ...
    +              if (!empty($book['pid'])) {
    +                $parent_node = Node::load($book['pid']);
    +                if (!empty($options['langcode']) && $parent_node->hasTranslation($options['langcode'])) {
    +                  $parent_node = \Drupal::service('entity.repository')->getTranslationFromContext($parent_node, $options['langcode']);
    +                }
    

    As said in #18, these 3 case check the if the translation exists, which is different from whether there is a langcode.

    I agree that the langcode options make sense, but not the hasTranslationCheck(), because there are use cases where language fallbacks are explicitly desired and this won't work with that.

    I'm also a bit confused about the issue title and summary and the patch. The patch is solely about book tokens, but the issue title and description is at least partially about entity reference tokens. I'm fine with making this specifically about book tokens, there's at least one other issue about entity reference fields, but then at least the issue title should be updated to reflect that.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland heikkiy Oulu

    We seem to be encountering a similar issue where we are using a taxonomy term token to generate a path for a content type.

    This works fine when we save the node but if we bulk generate paths it seems to replace the term with the default language term. Bulk saving the content fixes the issue again.

    But I agree with #23 that the issue title and summary and the patch seem to be doing a different thing. I am also a bit worried that #2648252: entity reference field token does not get translated โ†’ is marked as being a duplicate of this issue and this issue patch seems to solve it only for a certain use case.

    We can still test this patch and see if it fixes our issue.

  • ๐Ÿ‡ซ๐Ÿ‡ฎFinland heikkiy Oulu

    We found another similar issue which is not linked as a relationship here.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada joseph.olstad

    There's a few remaining nits to resolve with patch #21 as mentioned by @berdir.

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia pradhumanjainOSL

    pradhumanjain2311 โ†’ made their first commit to this issueโ€™s fork.

  • Open in Jenkins โ†’ Open on Drupal.org โ†’
    Core: 10.1.4 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 8 months ago
    78 pass
  • Pipeline finished with Success
    8 months ago
    Total: 385s
    #160902
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ

    It may work to use this token: [node:field_NAME:entity:title]

Production build 0.71.5 2024