Commit 5c0eb9b7 authored by alexpott's avatar alexpott

Issue #2090983 by plach, Berdir, yched:...

Issue #2090983 by plach, Berdir, yched: ContentEntityInterface::getTranslation() should throw an exception when an invalid language is specified
parent 4a992013
......@@ -70,7 +70,7 @@ abstract class ContentEntityBase extends Entity implements \IteratorAggregate, C
/**
* Local cache for the available language objects.
*
* @var array
* @var \Drupal\Core\Language\LanguageInterface[]
*/
protected $languages;
......@@ -737,22 +737,11 @@ public function getTranslation($langcode) {
if (isset($this->translations[$langcode]['entity'])) {
$translation = $this->translations[$langcode]['entity'];
}
else {
if (isset($this->translations[$langcode])) {
$translation = $this->initializeTranslation($langcode);
$this->translations[$langcode]['entity'] = $translation;
}
else {
// If we were given a valid language and there is no translation for it,
// we return a new one.
$this->getLanguages();
if (isset($this->languages[$langcode])) {
// If the entity or the requested language is not a configured
// language, we fall back to the entity itself, since in this case it
// cannot have translations.
$translation = !$this->languages[$this->defaultLangcode]->isLocked() && !$this->languages[$langcode]->isLocked() ? $this->addTranslation($langcode) : $this;
}
}
// Otherwise if an existing translation language was specified we need to
// instantiate the related translation.
elseif (isset($this->translations[$langcode])) {
$translation = $this->initializeTranslation($langcode);
$this->translations[$langcode]['entity'] = $translation;
}
if (empty($translation)) {
......@@ -823,10 +812,15 @@ public function hasTranslation($langcode) {
* {@inheritdoc}
*/
public function addTranslation($langcode, array $values = array()) {
// Make sure we do not attempt to create a translation if an invalid
// language is specified or the entity cannot be translated.
$this->getLanguages();
if (!isset($this->languages[$langcode]) || $this->hasTranslation($langcode)) {
if (!isset($this->languages[$langcode]) || $this->hasTranslation($langcode) || $this->languages[$langcode]->isLocked()) {
throw new \InvalidArgumentException("Invalid translation language ($langcode) specified.");
}
if ($this->languages[$this->defaultLangcode]->isLocked()) {
throw new \InvalidArgumentException("The entity cannot be translated since it is language neutral ({$this->defaultLangcode}).");
}
// Instantiate a new empty entity so default values will be populated in the
// specified language.
......
......@@ -173,7 +173,7 @@ protected function init(FormStateInterface $form_state) {
// language.
$this->initFormLangcodes($form_state);
$langcode = $this->getFormLangcode($form_state);
$this->entity = $this->entity->getTranslation($langcode);
$this->entity = $this->entity->hasTranslation($langcode) ? $this->entity->getTranslation($langcode) : $this->entity->addTranslation($langcode);
$form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation());
$this->setFormDisplay($form_display, $form_state);
......
......@@ -44,8 +44,7 @@ public function getTranslationLanguages($include_default = TRUE);
* Gets a translation of the data.
*
* The returned translation has to be of the same type than this typed data
* object. If the specified translation does not exist, a new one will be
* instantiated.
* object.
*
* @param $langcode
* The language code of the translation to get or
......@@ -54,6 +53,9 @@ public function getTranslationLanguages($include_default = TRUE);
*
* @return $this
* A typed data object for the translated data.
*
* @throws \InvalidArgumentException
* If an invalid or non-existing translation language is specified.
*/
public function getTranslation($langcode);
......@@ -86,6 +88,9 @@ public function hasTranslation($langcode);
* fields. Defaults to none.
*
* @return $this
*
* @throws \InvalidArgumentException
* If an invalid or existing translation language is specified.
*/
public function addTranslation($langcode, array $values = array());
......
......@@ -160,7 +160,7 @@ function testImageFieldSync() {
// items will be one less than the original values to check that only the
// translated ones will be preserved. In fact we want the same fids and
// items order for both languages.
$translation = $entity->getTranslation($langcode);
$translation = $entity->addTranslation($langcode);
for ($delta = 0; $delta < $this->cardinality - 1; $delta++) {
// Simulate a field reordering: items are shifted of one position ahead.
// The modulo operator ensures we start from the beginning after reaching
......
......@@ -117,7 +117,7 @@ protected function setUp() {
*/
protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
$entity = entity_load('entity_test', $entity_id, TRUE);
$items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_name);
$items = $entity->get($field_name);
$options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name);
return $this->editorSelector->getEditor($options['type'], $items);
}
......@@ -170,7 +170,7 @@ public function testMetadata() {
$entity = entity_load('entity_test', $entity->id());
// Verify metadata.
$items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($this->fieldName);
$items = $entity->get($this->fieldName);
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
$expected = array(
'access' => TRUE,
......@@ -209,7 +209,7 @@ public function testGetUntransformedTextCommand() {
// Verify AJAX response.
$controller = new EditorController();
$request = new Request();
$response = $controller->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_NOT_SPECIFIED, 'default');
$response = $controller->getUntransformedText($entity, $this->fieldName, LanguageInterface::LANGCODE_DEFAULT, 'default');
$expected = array(
array(
'command' => 'editorGetUntransformedText',
......
......@@ -46,11 +46,11 @@ function _generateTestFieldValues($cardinality) {
* The array of expected values.
* @param $langcode
* (Optional) The language code for the values. Defaults to
* \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
* \Drupal\Core\Language\LanguageInterface::LANGCODE_DEFAULT.
* @param $column
* (Optional) The name of the column to check. Defaults to 'value'.
*/
function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $column = 'value') {
function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_DEFAULT, $column = 'value') {
// Re-load the entity to make sure we have the latest changes.
\Drupal::entityManager()->getStorage($entity->getEntityTypeId())->resetCache(array($entity->id()));
$e = entity_load($entity->getEntityTypeId(), $entity->id());
......
......@@ -124,7 +124,8 @@ function testTranslatableFieldSaveLoad() {
$entity->langcode->value = reset($available_langcodes);
foreach ($available_langcodes as $langcode) {
$field_translations[$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode)->{$this->fieldName}->setValue($field_translations[$langcode]);
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$translation->{$this->fieldName}->setValue($field_translations[$langcode]);
}
// Save and reload the field translations.
......@@ -160,7 +161,8 @@ function testTranslatableFieldSaveLoad() {
$entity = entity_create($entity_type_id, $values);
foreach ($translation_langcodes as $langcode) {
$values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode, FALSE)->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
}
$field_langcodes = array_keys($entity->getTranslationLanguages());
......@@ -178,8 +180,9 @@ function testTranslatableFieldSaveLoad() {
$entity = entity_create($entity_type_id, $values);
foreach ($translation_langcodes as $langcode) {
$values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$entity->getTranslation($langcode)->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
$entity->getTranslation($langcode)->{$field_name_default}->setValue($empty_items);
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$translation->{$this->fieldName}->setValue($values[$this->fieldName][$langcode]);
$translation->{$field_name_default}->setValue($empty_items);
$values[$field_name_default][$langcode] = $empty_items;
}
......
......@@ -104,7 +104,8 @@ function testFieldFormTranslationRevisions() {
ksort($available_langcodes);
$entity->langcode->value = key($available_langcodes);
foreach ($available_langcodes as $langcode => $value) {
$entity->getTranslation($langcode)->{$field_name}->value = $value + 1;
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$translation->{$field_name}->value = $value + 1;
}
$entity->save();
......
......@@ -54,7 +54,9 @@ public function postSave($update) {
// Get the file IDs attached to the field before this update.
$field_name = $this->getFieldDefinition()->getName();
$original_ids = array();
$original_items = $entity->original->getTranslation($this->getLangcode())->$field_name;
$langcode = $this->getLangcode();
$original = $entity->original;
$original_items = $original->hasTranslation($langcode) ? $original->getTranslation($langcode)->{$field_name} : $original->{$field_name};
foreach ($original_items as $item) {
$original_ids[] = $item->target_id;
}
......
......@@ -107,7 +107,8 @@ protected function createTranslatedInstance(FieldItemInterface $item, $langcode)
unset($items[$delta]);
// Instead, create a new item for the entity in the requested language.
$entity_translation = $item->getEntity()->getTranslation($langcode);
$entity = $item->getEntity();
$entity_translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
$field_name = $item->getFieldDefinition()->getName();
return $entity_translation->get($field_name)->appendItem();
}
......
......@@ -58,7 +58,7 @@ public function testNormalize() {
$entity = entity_create('entity_test', $values);
$entity->save();
// Add an English value for name and entity reference properties.
$entity->getTranslation('en')->set('name', array(0 => array('value' => $translation_values['name'])));
$entity->addTranslation('en')->set('name', array(0 => array('value' => $translation_values['name'])));
$entity->getTranslation('en')->set('field_test_entity_reference', array(0 => $translation_values['field_test_entity_reference']));
$entity->save();
......
......@@ -100,7 +100,8 @@ public function createAccess($entity_bundle = NULL, AccountInterface $account =
protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) {
/** @var \Drupal\node\NodeInterface $node */
/** @var \Drupal\node\NodeInterface $translation */
$translation = $node->getTranslation($langcode);
$translation = $node->hasTranslation($langcode) ? $node->getTranslation($langcode) : $node;
// Fetch information from the node object if possible.
$status = $translation->isPublished();
$uid = $translation->getOwnerId();
......
......@@ -110,7 +110,7 @@ protected function setUp() {
'field_private' => array(array('value' => 0)),
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -121,7 +121,7 @@ protected function setUp() {
'field_private' => array(array('value' => 0)),
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -132,7 +132,7 @@ protected function setUp() {
'field_private' => array(array('value' => 1)),
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -143,7 +143,7 @@ protected function setUp() {
'field_private' => array(array('value' => 0)),
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......@@ -154,7 +154,7 @@ protected function setUp() {
'field_private' => array(array('value' => 1)),
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......@@ -165,7 +165,7 @@ protected function setUp() {
'field_private' => array(array('value' => 1)),
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......
......@@ -103,7 +103,7 @@ protected function setUp() {
'langcode' => 'hu',
'field_private' => array(array('value' => 0)),
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -113,7 +113,7 @@ protected function setUp() {
'langcode' => 'hu',
'field_private' => array(array('value' => 0)),
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......@@ -123,7 +123,7 @@ protected function setUp() {
'langcode' => 'hu',
'field_private' => array(array('value' => 1)),
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -133,7 +133,7 @@ protected function setUp() {
'langcode' => 'hu',
'field_private' => array(array('value' => 1)),
));
$translation = $node->getTranslation('ca');
$translation = $node->addTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......
......@@ -7,7 +7,6 @@
namespace Drupal\quickedit\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\quickedit\EditorSelector;
/**
......@@ -43,7 +42,7 @@ protected function setUp() {
*/
protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') {
$entity = entity_load('entity_test', $entity_id, TRUE);
$items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_name);
$items = $entity->get($field_name);
$options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name);
return $this->editorSelector->getEditor($options['type'], $items);
}
......
......@@ -7,7 +7,6 @@
namespace Drupal\quickedit\Tests;
use Drupal\Core\Language\LanguageInterface;
use Drupal\quickedit\EditorSelector;
use Drupal\quickedit\MetadataGenerator;
use Drupal\quickedit\Plugin\InPlaceEditorManager;
......@@ -101,7 +100,7 @@ public function testSimpleEntityType() {
$entity = entity_load('entity_test', $entity->id());
// Verify metadata for field 1.
$items_1 = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_1_name);
$items_1 = $entity->get($field_1_name);
$metadata_1 = $this->metadataGenerator->generateFieldMetadata($items_1, 'default');
$expected_1 = array(
'access' => TRUE,
......@@ -111,7 +110,7 @@ public function testSimpleEntityType() {
$this->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');
// Verify metadata for field 2.
$items_2 = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_2_name);
$items_2 = $entity->get($field_2_name);
$metadata_2 = $this->metadataGenerator->generateFieldMetadata($items_2, 'default');
$expected_2 = array(
'access' => TRUE,
......@@ -169,7 +168,7 @@ public function testEditorWithCustomMetadata() {
$entity = entity_load('entity_test', $entity->id());
// Verify metadata.
$items = $entity->getTranslation(LanguageInterface::LANGCODE_NOT_SPECIFIED)->get($field_name);
$items = $entity->get($field_name);
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
$expected = array(
'access' => TRUE,
......
......@@ -422,7 +422,7 @@ public function testRevisionChanged() {
'Changed flag of German translation is set when changing the German translation.'
);
$french = $entity->getTranslation('fr');
$french = $entity->addTranslation('fr');
$entity->setNewRevision();
$entity->save();
......
......@@ -137,7 +137,7 @@ function testEntityTranslationAccess() {
));
$entity->save();
$translation = $entity->getTranslation('bar');
$translation = $entity->addTranslation('bar');
$this->assertEntityAccess(array(
'view' => TRUE,
), $translation);
......
......@@ -130,7 +130,7 @@ protected function setUp() {
));
// Make sure the name is set for every language that we might create.
foreach (array('tr', 'pl') as $langcode) {
$entity->getTranslation($langcode)->name = $this->randomMachineName();
$entity->addTranslation($langcode)->name = $this->randomMachineName();
}
foreach (array_reverse(str_split(decbin($i))) as $key => $bit) {
if ($bit) {
......
......@@ -106,9 +106,10 @@ function testEntityFormLanguage() {
// Create a body translation and check the form language.
$langcode2 = $this->langcodes[1];
$node->getTranslation($langcode2)->title->value = $this->randomString();
$node->getTranslation($langcode2)->body->value = $this->randomMachineName(16);
$node->getTranslation($langcode2)->setOwnerId($web_user->id());
$translation = $node->addTranslation($langcode2);
$translation->title->value = $this->randomString();
$translation->body->value = $this->randomMachineName(16);
$translation->setOwnerId($web_user->id());
$node->save();
$this->drupalGet($langcode2 . '/node/' . $node->id() . '/edit');
$form_langcode = \Drupal::state()->get('entity_test.form_langcode');
......
......@@ -70,7 +70,7 @@ public function testTranslationRows() {
]);
$node->save();
$translation = $node->getTranslation('es');
$translation = $node->addTranslation('es');
$translation->title->value = 'example ES';
$translation->sticky->value = true;
$translation->save();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment