Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
As described in #2773329: Field token values are not fetched in the correct language if field translation is disabled, tokens for translated entities referred by an entity reference field are not in the correct language. For example, token [node:term_reference_field:entity:name] will be provided in the default language, even if the node provided is in a different (active) language and if a translation exists.
Proposed resolution
We need test coverage for translatable fields and entities. For instance, the :name token of an entity should be in the correct language. Once the bug is exposed, we can fix it.
Comment | File | Size | Author |
---|---|---|---|
#17 | provide_tokens_for-2772673-17.patch | 8.81 KB | Bambell |
Comments
Comment #2
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedTest only patch to expose the bug detailed in #2773329: Field token values are not fetched in the correct language if field translation is disabled.
Comment #5
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedPatch with test and suggested fix. Not entirely sure about
$entity_language_id !== 'und'
.Comment #6
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedSimplifying code and extending test coverage to fields.
Comment #7
Berdirthat's certainly also good to have but I actually meant to test fields on the node, both translatable and untranslatable.
So, for untranslatable, you can use just [test_term_reference] (which should show the label in the correct language).
And then add/use a translatable text field, e.g. test_translatable and then verify that you get the correct translation for both [test_translatable] and [test_translatable:value]
Comment #8
BerdirI think this field is translatable, that's why it currently works.
As discussed, lets use $langcode instead and use getTranslationFromContext() both on the referenced entity and also further up on the main entity that is passed in when doing other entity tokens.
node_tokens() does the same, for specific tokens.
Comment #9
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedUploading a failing patch to expose the problem in #8. When explicitly making untranslatable the taxonomy term reference field (
test_term_reference
), tests fail with :Comment #12
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedThis field token replacement function is frying my brain ;-P. I got this to work by passing
$langcode
to the recursed calls.Comment #13
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedAdding a test with "normal" (not entity reference) fields.
The big question now is whether we should try to fallback here or not ?
Comment #14
Berdirthis is not what I meant :)
A single field, with a translation. Not two fields :) Just fill out test_field in the existing node that you create and add a translation and then add assertions to testing that for the existing token asserts in each langugae and then also a case where you pass in $node but use langcode => 'de'. That's all.
Also, typo (a very consistent one ;)): deustch.
Comment #15
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedHere we go, hopefully I got it right now. Adding a text field to a node, translating that node and field and asserting that the field token is correct for the given node. If the langcode is specified during token replacement, then it has priority over the node's active langcode (had to make few additional changes for this).
Comment #16
BerdirReally close now :)
lets also add test_field:value, here and above.
can we add one more assert for test_term_reference:entity:term_field, just to make sure this works as well?
$active_langcode is only used if you go inside the if. so you can move that inside and assign $langcode directly in that case.
Comment #17
Bambell CreditAttribution: Bambell at MD Systems GmbH commentedHere we go. It wasn't working as intended with :value tokens (specified langcode was ignored).
Comment #19
BerdirCommitted.
Comment #20
Berdir