public function FieldTest::testMultilingualFields in Token 8
Test tokens for multilingual fields and entities.
File
- tests/
src/ Kernel/ FieldTest.php, line 608
Class
- FieldTest
- Tests field tokens.
Namespace
Drupal\Tests\token\KernelCode
public function testMultilingualFields() {
// Create an english term and add a german translation for it.
$term = $this
->createTerm($this->vocabulary, [
'name' => 'english-test-term',
'langcode' => 'en',
'term_field' => [
'value' => 'english-term-field-value',
'format' => $this->testFormat
->id(),
],
]);
$term
->addTranslation('de', [
'name' => 'german-test-term',
'term_field' => [
'value' => 'german-term-field-value',
'format' => $this->testFormat
->id(),
],
])
->save();
$german_term = $term
->getTranslation('de');
// Create an english node, add a german translation for it and add the
// english term to the english node's entity reference field and the
// german term to the german's entity reference field.
$node = Node::create([
'title' => 'english-node-title',
'type' => 'article',
'test_term_reference' => [
'target_id' => $term
->id(),
],
'test_field' => [
'value' => 'test-english-field',
'format' => $this->testFormat
->id(),
],
]);
$node
->addTranslation('de', [
'title' => 'german-node-title',
'test_term_reference' => [
'target_id' => $german_term
->id(),
],
'test_field' => [
'value' => 'test-german-field',
'format' => $this->testFormat
->id(),
],
])
->save();
// Verify the :title token of the english node and the :name token of the
// english term it refers to. Also verify the value of the term's field.
$this
->assertTokens('node', [
'node' => $node,
], [
'title' => 'english-node-title',
'test_term_reference:entity:name' => 'english-test-term',
'test_term_reference:entity:term_field:value' => 'english-term-field-value',
'test_term_reference:entity:term_field' => 'english-term-field-value',
'test_field' => 'test-english-field',
'test_field:value' => 'test-english-field',
]);
// Same test for the german node and its german term.
$german_node = $node
->getTranslation('de');
$this
->assertTokens('node', [
'node' => $german_node,
], [
'title' => 'german-node-title',
'test_term_reference:entity:name' => 'german-test-term',
'test_term_reference:entity:term_field:value' => 'german-term-field-value',
'test_term_reference:entity:term_field' => 'german-term-field-value',
'test_field' => 'test-german-field',
'test_field:value' => 'test-german-field',
]);
// If the langcode is specified, it should have priority over the node's
// active language.
$tokens = [
'test_field' => 'test-german-field',
'test_field:value' => 'test-german-field',
'test_term_reference:entity:term_field' => 'german-term-field-value',
'test_term_reference:entity:term_field:value' => 'german-term-field-value',
];
$this
->assertTokens('node', [
'node' => $node,
], $tokens, [
'langcode' => 'de',
]);
}