Commit 3a7bfeac authored by catch's avatar catch

Issue #1983554 by plach, effulgentsia, Berdir: Remove BC-mode from EntityNG .

parent 2de01f4f
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
use Drupal\Core\Database\Connection; use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Language\Language;
use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Uuid\Uuid; use Drupal\Component\Uuid\Uuid;
use Drupal\field\FieldInfo; use Drupal\field\FieldInfo;
...@@ -569,15 +570,20 @@ protected function doLoadFieldItems($entities, $age) { ...@@ -569,15 +570,20 @@ protected function doLoadFieldItems($entities, $age) {
} }
// Load field data. // Load field data.
$all_langcodes = array_keys(language_list());
foreach ($fields as $field_name => $field) { foreach ($fields as $field_name => $field) {
$table = $load_current ? static::_fieldTableName($field) : static::_fieldRevisionTableName($field); $table = $load_current ? static::_fieldTableName($field) : static::_fieldRevisionTableName($field);
// If the field is translatable ensure that only values having valid
// languages are retrieved. Since we are loading values for multiple
// entities, we cannot limit the query to the available translations.
$langcodes = $field['translatable'] ? $all_langcodes : array(Language::LANGCODE_NOT_SPECIFIED);
$results = $this->database->select($table, 't') $results = $this->database->select($table, 't')
->fields('t') ->fields('t')
->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN') ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
->condition('langcode', field_available_languages($this->entityType, $field), 'IN')
->orderBy('delta')
->condition('deleted', 0) ->condition('deleted', 0)
->condition('langcode', $langcodes, 'IN')
->orderBy('delta')
->execute(); ->execute();
$delta_count = array(); $delta_count = array();
...@@ -597,7 +603,7 @@ protected function doLoadFieldItems($entities, $age) { ...@@ -597,7 +603,7 @@ protected function doLoadFieldItems($entities, $age) {
} }
// Add the item to the field values for the entity. // Add the item to the field values for the entity.
$entities[$row->entity_id]->{$field_name}[$row->langcode][] = $item; $entities[$row->entity_id]->getTranslation($row->langcode)->{$field_name}[$delta_count[$row->entity_id][$row->langcode]] = $item;
$delta_count[$row->entity_id][$row->langcode]++; $delta_count[$row->entity_id][$row->langcode]++;
} }
} }
...@@ -621,30 +627,20 @@ protected function doSaveFieldItems(EntityInterface $entity, $update) { ...@@ -621,30 +627,20 @@ protected function doSaveFieldItems(EntityInterface $entity, $update) {
$table_name = static::_fieldTableName($field); $table_name = static::_fieldTableName($field);
$revision_name = static::_fieldRevisionTableName($field); $revision_name = static::_fieldRevisionTableName($field);
$all_langcodes = field_available_languages($entity_type, $field);
$field_langcodes = array_intersect($all_langcodes, array_keys((array) $entity->$field_name));
// Delete and insert, rather than update, in case a value was added. // Delete and insert, rather than update, in case a value was added.
if ($update) { if ($update) {
// Delete language codes present in the incoming $entity->$field_name.
// Delete all language codes if $entity->$field_name is empty.
$langcodes = !empty($entity->$field_name) ? $field_langcodes : $all_langcodes;
if ($langcodes) {
// Only overwrite the field's base table if saving the default revision // Only overwrite the field's base table if saving the default revision
// of an entity. // of an entity.
if ($entity->isDefaultRevision()) { if ($entity->isDefaultRevision()) {
$this->database->delete($table_name) $this->database->delete($table_name)
->condition('entity_id', $id) ->condition('entity_id', $id)
->condition('langcode', $langcodes, 'IN')
->execute(); ->execute();
} }
$this->database->delete($revision_name) $this->database->delete($revision_name)
->condition('entity_id', $id) ->condition('entity_id', $id)
->condition('revision_id', $vid) ->condition('revision_id', $vid)
->condition('langcode', $langcodes, 'IN')
->execute(); ->execute();
} }
}
// Prepare the multi-insert query. // Prepare the multi-insert query.
$do_insert = FALSE; $do_insert = FALSE;
...@@ -655,8 +651,12 @@ protected function doSaveFieldItems(EntityInterface $entity, $update) { ...@@ -655,8 +651,12 @@ protected function doSaveFieldItems(EntityInterface $entity, $update) {
$query = $this->database->insert($table_name)->fields($columns); $query = $this->database->insert($table_name)->fields($columns);
$revision_query = $this->database->insert($revision_name)->fields($columns); $revision_query = $this->database->insert($revision_name)->fields($columns);
foreach ($field_langcodes as $langcode) { $langcodes = $field['translatable'] ? array_keys($entity->getTranslationLanguages()) : array(Language::LANGCODE_NOT_SPECIFIED);
$items = (array) $entity->{$field_name}[$langcode]; foreach ($langcodes as $langcode) {
$items = $entity->getTranslation($langcode)->{$field_name}->getValue();
if (!isset($items)) {
continue;
}
$delta_count = 0; $delta_count = 0;
foreach ($items as $delta => $item) { foreach ($items as $delta => $item) {
// We now know we have someting to insert. // We now know we have someting to insert.
......
...@@ -330,9 +330,6 @@ protected function attachPropertyData(array &$entities, $revision_id = FALSE) { ...@@ -330,9 +330,6 @@ protected function attachPropertyData(array &$entities, $revision_id = FALSE) {
public function save(EntityInterface $entity) { public function save(EntityInterface $entity) {
$transaction = $this->database->startTransaction(); $transaction = $this->database->startTransaction();
try { try {
// Ensure we are dealing with the actual entity.
$entity = $entity->getNGEntity();
// Sync the changes made in the fields array to the internal values array. // Sync the changes made in the fields array to the internal values array.
$entity->updateOriginalValues(); $entity->updateOriginalValues();
...@@ -572,11 +569,6 @@ public function delete(array $entities) { ...@@ -572,11 +569,6 @@ public function delete(array $entities) {
$entity_class = $this->entityClass; $entity_class = $this->entityClass;
$entity_class::preDelete($this, $entities); $entity_class::preDelete($this, $entities);
// Ensure we are dealing with the actual entities.
foreach ($entities as $id => $entity) {
$entities[$id] = $entity->getNGEntity();
}
foreach ($entities as $entity) { foreach ($entities as $entity) {
$this->invokeHook('predelete', $entity); $this->invokeHook('predelete', $entity);
} }
......
...@@ -412,20 +412,6 @@ public function getExportProperties() { ...@@ -412,20 +412,6 @@ public function getExportProperties() {
return array(); return array();
} }
/**
* Implements \Drupal\Core\Entity\EntityInterface::getBCEntity().
*/
public function getBCEntity() {
return $this;
}
/**
* Implements \Drupal\Core\Entity\EntityInterface::getNGEntity().
*/
public function getNGEntity() {
return $this;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
This diff is collapsed.
...@@ -274,33 +274,12 @@ public function validate(array $form, array &$form_state) { ...@@ -274,33 +274,12 @@ public function validate(array $form, array &$form_state) {
$violations = array(); $violations = array();
// @todo Simplify when all entity types are converted to EntityNG.
if ($entity instanceof EntityNG) {
foreach ($entity as $field_name => $field) { foreach ($entity as $field_name => $field) {
$field_violations = $field->validate(); $field_violations = $field->validate();
if (count($field_violations)) { if (count($field_violations)) {
$violations[$field_name] = $field_violations; $violations[$field_name] = $field_violations;
} }
} }
}
else {
// For BC entities, iterate through each field instance and
// instantiate NG items objects manually.
$definitions = \Drupal::entityManager()->getFieldDefinitions($entity_type, $entity->bundle());
foreach (field_info_instances($entity_type, $entity->bundle()) as $field_name => $instance) {
$langcode = field_is_translatable($entity_type, $instance->getField()) ? $entity_langcode : Language::LANGCODE_NOT_SPECIFIED;
// Create the field object.
$items = isset($entity->{$field_name}[$langcode]) ? $entity->{$field_name}[$langcode] : array();
// @todo Exception : calls setValue(), tries to set the 'formatted'
// property.
$field = \Drupal::typedData()->create($definitions[$field_name], $items, $field_name, $entity);
$field_violations = $field->validate();
if (count($field_violations)) {
$violations[$field->getName()] = $field_violations;
}
}
}
// Map errors back to form elements. // Map errors back to form elements.
if ($violations) { if ($violations) {
......
...@@ -290,26 +290,6 @@ public function isDefaultRevision($new_value = NULL); ...@@ -290,26 +290,6 @@ public function isDefaultRevision($new_value = NULL);
*/ */
public function getExportProperties(); public function getExportProperties();
/**
* Gets a backward compatibility decorator entity.
*
* @return \Drupal\Core\Entity\EntityInterface
* The backward compatible entity.
*
* @see \Drupal\Core\Entity\EntityInterface::getNGEntity()
*/
public function getBCEntity();
/**
* Removes any possible (backward compatibility) decorator in use.
*
* @return \Drupal\Core\Entity\EntityInterface
* The original, not backward compatible entity object.
*
* @see \Drupal\Core\Entity\EntityInterface::getBCEntity()
*/
public function getNGEntity();
/** /**
* Returns the translation support status. * Returns the translation support status.
* *
......
...@@ -67,13 +67,6 @@ class EntityNG extends Entity { ...@@ -67,13 +67,6 @@ class EntityNG extends Entity {
*/ */
protected $fields = array(); protected $fields = array();
/**
* An instance of the backward compatibility decorator.
*
* @var EntityBCDecorator
*/
protected $bcEntity;
/** /**
* Local cache for the entity language. * Local cache for the entity language.
* *
...@@ -306,13 +299,6 @@ protected function getTranslatedField($property_name, $langcode) { ...@@ -306,13 +299,6 @@ protected function getTranslatedField($property_name, $langcode) {
if (isset($this->values[$property_name][$langcode])) { if (isset($this->values[$property_name][$langcode])) {
$value = $this->values[$property_name][$langcode]; $value = $this->values[$property_name][$langcode];
} }
// @todo Remove this once the BC decorator is gone.
elseif ($property_name != 'langcode' && $langcode == Language::LANGCODE_DEFAULT) {
$default_langcode = $this->language()->id;
if (isset($this->values[$property_name][$default_langcode])) {
$value = $this->values[$property_name][$default_langcode];
}
}
$field = \Drupal::typedData()->getPropertyInstance($this, $property_name, $value); $field = \Drupal::typedData()->getPropertyInstance($this, $property_name, $value);
$field->setLangcode($langcode); $field->setLangcode($langcode);
$this->fields[$property_name][$langcode] = $field; $this->fields[$property_name][$langcode] = $field;
...@@ -570,6 +556,10 @@ protected function initializeTranslation($langcode) { ...@@ -570,6 +556,10 @@ protected function initializeTranslation($langcode) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function hasTranslation($langcode) { public function hasTranslation($langcode) {
$default_language = $this->language ?: $this->getDefaultLanguage();
if ($langcode == $default_language->id) {
$langcode = Language::LANGCODE_DEFAULT;
}
return !empty($this->translations[$langcode]['status']); return !empty($this->translations[$langcode]['status']);
} }
...@@ -615,10 +605,10 @@ public function addTranslation($langcode, array $values = array()) { ...@@ -615,10 +605,10 @@ public function addTranslation($langcode, array $values = array()) {
*/ */
public function removeTranslation($langcode) { public function removeTranslation($langcode) {
if (isset($this->translations[$langcode]) && $langcode != Language::LANGCODE_DEFAULT && $langcode != $this->getDefaultLanguage()->id) { if (isset($this->translations[$langcode]) && $langcode != Language::LANGCODE_DEFAULT && $langcode != $this->getDefaultLanguage()->id) {
foreach ($this->getPropertyDefinitions() as $definition) { foreach ($this->getPropertyDefinitions() as $name => $definition) {
if (!empty($definition['translatable'])) { if (!empty($definition['translatable'])) {
unset($this->values[$langcode]); unset($this->values[$name][$langcode]);
unset($this->fields[$langcode]); unset($this->fields[$name][$langcode]);
} }
} }
$this->translations[$langcode]['status'] = static::TRANSLATION_REMOVED; $this->translations[$langcode]['status'] = static::TRANSLATION_REMOVED;
...@@ -663,18 +653,6 @@ public function translations() { ...@@ -663,18 +653,6 @@ public function translations() {
return $this->getTranslationLanguages(FALSE); return $this->getTranslationLanguages(FALSE);
} }
/**
* Overrides Entity::getBCEntity().
*/
public function getBCEntity() {
if (!isset($this->bcEntity)) {
// Initialize field definitions so that we can pass them by reference.
$this->getPropertyDefinitions();
$this->bcEntity = new EntityBCDecorator($this, $this->fieldDefinitions);
}
return $this->bcEntity;
}
/** /**
* Updates the original values with the interim changes. * Updates the original values with the interim changes.
*/ */
...@@ -695,8 +673,8 @@ public function updateOriginalValues() { ...@@ -695,8 +673,8 @@ public function updateOriginalValues() {
/** /**
* Implements the magic method for setting object properties. * Implements the magic method for setting object properties.
* *
* Uses default language always. * @todo: A lot of code still uses non-fields (e.g. $entity->content in render
* For compatibility mode to work this must return a reference. * controllers by reference. Clean that up.
*/ */
public function &__get($name) { public function &__get($name) {
// If this is an entity field, handle it accordingly. We first check whether // If this is an entity field, handle it accordingly. We first check whether
...@@ -712,11 +690,6 @@ public function &__get($name) { ...@@ -712,11 +690,6 @@ public function &__get($name) {
$return = $this->getTranslatedField($name, $this->activeLangcode); $return = $this->getTranslatedField($name, $this->activeLangcode);
return $return; return $return;
} }
// Allow the EntityBCDecorator to directly access the values and fields.
// @todo: Remove once the EntityBCDecorator gets removed.
if ($name == 'values' || $name == 'fields') {
return $this->$name;
}
// Else directly read/write plain values. That way, non-field entity // Else directly read/write plain values. That way, non-field entity
// properties can always be accessed directly. // properties can always be accessed directly.
if (!isset($this->values[$name])) { if (!isset($this->values[$name])) {
...@@ -743,6 +716,11 @@ public function __set($name, $value) { ...@@ -743,6 +716,11 @@ public function __set($name, $value) {
elseif ($this->getPropertyDefinition($name)) { elseif ($this->getPropertyDefinition($name)) {
$this->getTranslatedField($name, $this->activeLangcode)->setValue($value); $this->getTranslatedField($name, $this->activeLangcode)->setValue($value);
} }
// The translations array is unset when cloning the entity object, we just
// need to restore it.
elseif ($name == 'translations') {
$this->translations = $value;
}
// Else directly read/write plain values. That way, fields not yet converted // Else directly read/write plain values. That way, fields not yet converted
// to the entity field API can always be directly accessed. // to the entity field API can always be directly accessed.
else { else {
...@@ -804,8 +782,6 @@ public function createDuplicate() { ...@@ -804,8 +782,6 @@ public function createDuplicate() {
* Magic method: Implements a deep clone. * Magic method: Implements a deep clone.
*/ */
public function __clone() { public function __clone() {
$this->bcEntity = NULL;
// Avoid deep-cloning when we are initializing a translation object, since // Avoid deep-cloning when we are initializing a translation object, since
// it will represent the same entity, only with a different active language. // it will represent the same entity, only with a different active language.
if (!$this->translationInitialize) { if (!$this->translationInitialize) {
...@@ -815,7 +791,15 @@ public function __clone() { ...@@ -815,7 +791,15 @@ public function __clone() {
$this->fields[$name][$langcode]->setContext($name, $this); $this->fields[$name][$langcode]->setContext($name, $this);
} }
} }
// Ensure the translations array is actually cloned by removing the
// original reference and re-creating its values.
$this->clearTranslationCache(); $this->clearTranslationCache();
$translations = $this->translations;
unset($this->translations);
// This will trigger the magic setter as the translations array is
// undefined now.
$this->translations = $translations;
} }
} }
......
...@@ -42,11 +42,6 @@ protected function loadFieldItems(array $entities, $age) { ...@@ -42,11 +42,6 @@ protected function loadFieldItems(array $entities, $age) {
$info = entity_get_info($this->entityType); $info = entity_get_info($this->entityType);
$use_cache = $load_current && $info['field_cache']; $use_cache = $load_current && $info['field_cache'];
// Ensure we are working with a BC mode entity.
foreach ($entities as $id => $entity) {
$entities[$id] = $entity->getBCEntity();
}
// Assume all entities will need to be queried. Entities found in the cache // Assume all entities will need to be queried. Entities found in the cache
// will be removed from the list. // will be removed from the list.
$queried_entities = $entities; $queried_entities = $entities;
...@@ -65,8 +60,13 @@ protected function loadFieldItems(array $entities, $age) { ...@@ -65,8 +60,13 @@ protected function loadFieldItems(array $entities, $age) {
$cid = "field:{$this->entityType}:$id"; $cid = "field:{$this->entityType}:$id";
if (isset($cache[$cid])) { if (isset($cache[$cid])) {
unset($queried_entities[$id]); unset($queried_entities[$id]);
foreach ($cache[$cid]->data as $field_name => $values) { foreach ($cache[$cid]->data as $langcode => $values) {
$entity->$field_name = $values; $translation = $entity->getTranslation($langcode);
// We do not need to worry about field translatability here, the
// translation object will manage that automatically.
foreach ($values as $field_name => $items) {
$translation->$field_name = $items;
}
} }
} }
} }
...@@ -87,8 +87,11 @@ protected function loadFieldItems(array $entities, $age) { ...@@ -87,8 +87,11 @@ protected function loadFieldItems(array $entities, $age) {
foreach ($queried_entities as $id => $entity) { foreach ($queried_entities as $id => $entity) {
$data = array(); $data = array();
$instances = field_info_instances($this->entityType, $entity->bundle()); $instances = field_info_instances($this->entityType, $entity->bundle());
foreach ($entity->getTranslationLanguages() as $langcode => $language) {
$translation = $entity->getTranslation($langcode);
foreach ($instances as $instance) { foreach ($instances as $instance) {
$data[$instance['field_name']] = $queried_entities[$id]->{$instance['field_name']}; $data[$langcode][$instance['field_name']] = $translation->{$instance['field_name']}->getValue();
}
} }
$cid = "field:{$this->entityType}:$id"; $cid = "field:{$this->entityType}:$id";
cache('field')->set($cid, $data); cache('field')->set($cid, $data);
...@@ -110,9 +113,6 @@ protected function loadFieldItems(array $entities, $age) { ...@@ -110,9 +113,6 @@ protected function loadFieldItems(array $entities, $age) {
* TRUE if the entity is being updated, FALSE if it is being inserted. * TRUE if the entity is being updated, FALSE if it is being inserted.
*/ */
protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { protected function saveFieldItems(EntityInterface $entity, $update = TRUE) {
// Ensure we are working with a BC mode entity.
$entity = $entity->getBCEntity();
$this->doSaveFieldItems($entity, $update); $this->doSaveFieldItems($entity, $update);
if ($update) { if ($update) {
...@@ -134,9 +134,6 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) { ...@@ -134,9 +134,6 @@ protected function saveFieldItems(EntityInterface $entity, $update = TRUE) {
* The entity. * The entity.
*/ */
protected function deleteFieldItems(EntityInterface $entity) { protected function deleteFieldItems(EntityInterface $entity) {
// Ensure we are working with a BC mode entity.
$entity = $entity->getBCEntity();
$this->doDeleteFieldItems($entity); $this->doDeleteFieldItems($entity);
$entity_info = $entity->entityInfo(); $entity_info = $entity->entityInfo();
...@@ -156,7 +153,7 @@ protected function deleteFieldItems(EntityInterface $entity) { ...@@ -156,7 +153,7 @@ protected function deleteFieldItems(EntityInterface $entity) {
* The entity. It must have a revision ID attribute. * The entity. It must have a revision ID attribute.
*/ */
protected function deleteFieldItemsRevision(EntityInterface $entity) { protected function deleteFieldItemsRevision(EntityInterface $entity) {
$this->doDeleteFieldItemsRevision($entity->getBCEntity()); $this->doDeleteFieldItemsRevision($entity);
} }
/** /**
......
...@@ -99,10 +99,6 @@ public function setValue($value, $notify = TRUE) { ...@@ -99,10 +99,6 @@ public function setValue($value, $notify = TRUE) {
// Both the entity ID and the entity object may be passed as value. The // Both the entity ID and the entity object may be passed as value. The
// reference may also be unset by passing NULL as value. // reference may also be unset by passing NULL as value.
if (!isset($value) || $value instanceof EntityInterface) { if (!isset($value) || $value instanceof EntityInterface) {
// Ensure we reference a NG Entity object.
if (isset($value)) {
$value = $value->getNGEntity();
}
$this->target = $value; $this->target = $value;
} }
elseif (!is_scalar($value) || empty($this->definition['constraints']['EntityType'])) { elseif (!is_scalar($value) || empty($this->definition['constraints']['EntityType'])) {
......
...@@ -142,8 +142,7 @@ public function addField($field, $type, $langcode) { ...@@ -142,8 +142,7 @@ public function addField($field, $type, $langcode) {
} }
$entity = $entity_manager $entity = $entity_manager
->getStorageController($entity_type) ->getStorageController($entity_type)
->create($values) ->create($values);
->getNGEntity();
$propertyDefinitions = $entity->$field_name->getPropertyDefinitions(); $propertyDefinitions = $entity->$field_name->getPropertyDefinitions();
// If the column is not yet known, ie. the // If the column is not yet known, ie. the
...@@ -196,8 +195,7 @@ public function addField($field, $type, $langcode) { ...@@ -196,8 +195,7 @@ public function addField($field, $type, $langcode) {
} }
$entity = $entity_manager $entity = $entity_manager
->getStorageController($entity_type) ->getStorageController($entity_type)
->create($values) ->create($values);
->getNGEntity();
$propertyDefinitions = $entity->$specifier->getPropertyDefinitions(); $propertyDefinitions = $entity->$specifier->getPropertyDefinitions();
$relationship_specifier = $specifiers[$key + 1]; $relationship_specifier = $specifiers[$key + 1];
$next_index_prefix = $relationship_specifier; $next_index_prefix = $relationship_specifier;
......
...@@ -610,22 +610,12 @@ function content_translation_form_alter(array &$form, array &$form_state) { ...@@ -610,22 +610,12 @@ function content_translation_form_alter(array &$form, array &$form_state) {
// Handle fields shared between translations when there is at least one // Handle fields shared between translations when there is at least one
// translation available or a new one is being created. // translation available or a new one is being created.
if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) { if (!$entity->isNew() && (!isset($translations[$form_langcode]) || count($translations) > 1)) {
$entity = $entity->getNGEntity();
if ($entity instanceof EntityNG) {
foreach ($entity->getPropertyDefinitions() as $property_name => $definition) { foreach ($entity->getPropertyDefinitions() as $property_name => $definition) {
if (isset($form[$property_name])) { if (isset($form[$property_name])) {
$form[$property_name]['#multilingual'] = !empty($definition['translatable']); $form[$property_name]['#multilingual'] = !empty($definition['translatable']);
} }
} }
} }
else {
foreach (field_info_instances($entity->entityType(), $entity->bundle()) as $instance) {
$field_name = $instance['field_name'];
$field = $instance->getField();
$form[$field_name]['#multilingual'] = !empty($field['translatable']);
}
}
}
} }
} }
...@@ -647,18 +637,7 @@ function content_translation_field_language_alter(&$display_language, $context) ...@@ -647,18 +637,7 @@ function content_translation_field_language_alter(&$display_language, $context)
foreach ($entity->translation as $langcode => $translation) { foreach ($entity->translation as $langcode => $translation) {
if ($langcode == $context['langcode'] || !content_translation_view_access($entity, $langcode)) { if ($langcode == $context['langcode'] || !content_translation_view_access($entity, $langcode)) {
// Unset unaccessible field translations: if the field is untranslatable $entity->removeTranslation($langcode);
// unsetting a language different from Language::LANGCODE_NOT_SPECIFIED has no
// effect.
foreach ($instances as $instance) {
// @todo BC entities have the same value accessibile both with the
// entity language and with Language::LANGCODE_DEFAULT. We need need to unset
// both until we remove the BC layer.
if ($langcode == $entity_langcode) {
unset($entity->{$instance['field_name']}[Language::LANGCODE_DEFAULT]);
}
unset($entity->{$instance['field_name']}[$langcode]);
}
} }
} }
......
...@@ -236,23 +236,8 @@ function content_translation_edit_page(EntityInterface $entity, Language $langua ...@@ -236,23 +236,8 @@ function content_translation_edit_page(EntityInterface $entity, Language $langua
* The language to be used as target. * The language to be used as target.
*/ */
function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) { function content_translation_prepare_translation(EntityInterface $entity, Language $source, Language $target) {
// @todo Unify field and property handling.
$entity = $entity->getNGEntity();
if ($entity instanceof EntityNG) {
$source_translation = $entity->getTranslation($source->id); $source_translation = $entity->getTranslation($source->id);
$entity->addTranslation($target->id, $source_translation->getPropertyValues()); $entity->addTranslation($target->id, $source_translation->getPropertyValues());
}
else {
$instances = field_info_instances($entity->entityType(), $entity->bundle());
foreach ($instances as $field_name => $instance) {
$field = $instance->getField();
if (!empty($field['translatable'])) {
$value = $entity->get($field_name);
$value[$target->id] = isset($value[$source->id]) ? $value[$source->id] : array();
$entity->set($field_name, $value);
}
}
}
} }
/** /**
......
...@@ -37,12 +37,6 @@ public function __construct(EntityManager $entityManager) { ...@@ -37,12 +37,6 @@ public function __construct(EntityManager $entityManager) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function synchronizeFields(EntityInterface $entity, $sync_langcode, $original_langcode = NULL) { public function synchronizeFields(EntityInterface $entity, $sync_langcode, $original_langcode = NULL) {
// Field synchronization is only supported for NG entities.
$entity = $entity->getNGEntity();
if (!($entity instanceof EntityNG)) {
return;
}
$translations = $entity->getTranslationLanguages(); $translations = $entity->getTranslationLanguages();
// If we have no information about what to sync to, if we are creating a new // If we have no information about what to sync to, if we are creating a new
......