diff --git a/tests/src/Kernel/FieldTest.php b/tests/src/Kernel/FieldTest.php index 732657c..dc42a07 100644 --- a/tests/src/Kernel/FieldTest.php +++ b/tests/src/Kernel/FieldTest.php @@ -16,6 +16,7 @@ use Drupal\contact\Entity\Message; use Drupal\Component\Utility\Html; use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\taxonomy\Tests\TaxonomyTestTrait; +use Drupal\language\Entity\ConfigurableLanguage; /** * Tests field tokens. @@ -45,7 +46,7 @@ class FieldTest extends KernelTestBase { * * @var array */ - public static $modules = ['node', 'text', 'field', 'filter', 'contact', 'options', 'taxonomy']; + public static $modules = ['node', 'text', 'field', 'filter', 'contact', 'options', 'taxonomy', 'language']; /** * {@inheritdoc} @@ -156,6 +157,13 @@ class FieldTest extends KernelTestBase { 'bundle' => $this->vocabulary->id(), ]); $field->save(); + + // Add a second language. + $language = ConfigurableLanguage::create([ + 'id' => 'de', + 'label' => 'Deutsch', + ]); + $language->save(); } /** @@ -509,4 +517,48 @@ class FieldTest extends KernelTestBase { ]); } + /** + * Test tokens for multilingual fields and entities. + */ + public function testMultilingualFields() { + // Create an english term and add a deutsch translation for it. + $term = $this->createTerm($this->vocabulary, [ + 'name' => 'english-test-term', + 'langcode' => 'en', + ]); + $term->addTranslation('de', ['name' => 'deutsch-test-term'])->save(); + $deutsch_term = $term->getTranslation('de'); + + // Create an english node, add a deutsch translation for it and add the + // english term to the english node's entity reference field and the + // deutsch term to the deutsch's entity reference field. + $node = Node::create([ + 'title' => 'english-node-title', + 'type' => 'article', + 'test_term_reference' => [ + 'target_id' => $term->id(), + ], + ]); + $node->addTranslation('de', [ + 'title' => 'deutsch-node-title', + 'test_term_reference' => [ + 'target_id' => $deutsch_term->id(), + ], + ])->save(); + + // Verify the :title token of the english node and the :name token of the + // english term it refers to. + $this->assertTokens('node', ['node' => $node], [ + 'title' => 'english-node-title', + 'test_term_reference:entity:name' => 'english-test-term', + ]); + + // Same test for the deutsch node and its deutsch term. + $deutsch_node = $node->getTranslation('de'); + $this->assertTokens('node', ['node' => $deutsch_node], [ + 'title' => 'deutsch-node-title', + 'test_term_reference:entity:name' => 'deutsch-test-term', + ]); + } + } diff --git a/token.tokens.inc b/token.tokens.inc index 5b39039..2193403 100644 --- a/token.tokens.inc +++ b/token.tokens.inc @@ -1450,7 +1450,13 @@ function field_tokens($type, $tokens, array $data = array(), array $options = ar if (isset($field_item->$property_name) && ($field_item->$property_name instanceof FieldableEntityInterface)) { // Entity reference field. + $active_langcode = $field_item->getEntity()->language()->getId(); $entity = $field_item->$property_name; + $entity_language_id = $entity->language()->getId(); + // Obtain the referenced entity with the correct language. + if (($entity_language_id !== $active_langcode) && ($entity_language_id !== 'und') && $entity->hasTranslation($active_langcode)) { + $entity = $entity->getTranslation($active_langcode); + } if (count($parts) > 1) { $field_tokens = \Drupal::token()->findWithPrefix($filtered_tokens, $property_name); $token_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity->getEntityTypeId(), TRUE);