$this->
fail('Adding a translation to a language-neutral entity results in an error.'
);
} catch (\InvalidArgumentException
$e) { // Expected exception; just continue testing.
} // Now, make the entity language-specific by assigning a language and test
// translating it.
$default_langcode =
$this->langcodes
[0
];
$entity->
{$langcode_key}->value =
$default_langcode;
$entity->
{$this->fieldName
} =
[];
$this->
assertEquals(\Drupal::
languageManager()->
getLanguage($this->langcodes
[0
]),
$entity->
language(),
new FormattableMarkup('%entity_type: Entity language retrieved.',
['%entity_type' =>
$entity_type]));
$this->
assertEmpty($entity->
getTranslationLanguages(FALSE
),
new FormattableMarkup('%entity_type: No translations are available',
['%entity_type' =>
$entity_type]));
// Set the value in default language.
$entity->
set($this->fieldName,
[0 =>
['value' => 'default value'
]]);
// Get the value.
$field =
$entity->
get($this->fieldName
);
$this->
assertEquals('default value',
$field->value,
new FormattableMarkup('%entity_type: Untranslated value retrieved.',
['%entity_type' =>
$entity_type]));
$this->
assertEquals($default_langcode,
$field->
getLangcode(),
new FormattableMarkup('%entity_type: Field object has the expected langcode.',
['%entity_type' =>
$entity_type]));
// Set a translation.
$entity->
addTranslation($this->langcodes
[1
])->
set($this->fieldName,
[0 =>
['value' => 'translation 1'
]]);