Commit ccc65da7 authored by xjm's avatar xjm

Issue #2854926 by xjm, borisson_, boaloysius: Remove unneeded control...

Issue #2854926 by xjm, borisson_, boaloysius: Remove unneeded control structures in ContentEntityBase
parent 7e506626
...@@ -1014,10 +1014,9 @@ public function __isset($name) { ...@@ -1014,10 +1014,9 @@ public function __isset($name) {
if ($this->hasField($name)) { if ($this->hasField($name)) {
return TRUE; return TRUE;
} }
// For non-field properties, check the internal values. // For non-field properties, check the internal values.
else { return isset($this->values[$name]);
return isset($this->values[$name]);
}
} }
/** /**
...@@ -1027,11 +1026,11 @@ public function __unset($name) { ...@@ -1027,11 +1026,11 @@ public function __unset($name) {
// Unsetting a field means emptying it. // Unsetting a field means emptying it.
if ($this->hasField($name)) { if ($this->hasField($name)) {
$this->get($name)->setValue(array()); $this->get($name)->setValue(array());
return TRUE;
} }
// For non-field properties, unset the internal value. // For non-field properties, unset the internal value.
else { unset($this->values[$name]);
unset($this->values[$name]);
}
} }
/** /**
...@@ -1067,60 +1066,62 @@ public function createDuplicate() { ...@@ -1067,60 +1066,62 @@ public function createDuplicate() {
public function __clone() { public function __clone() {
// 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) {
// The translation is a different object, and needs its own TypedData return TRUE;
// adapter object. }
$this->typedData = NULL;
$definitions = $this->getFieldDefinitions(); // The translation is a different object, and needs its own TypedData
// adapter object.
// The translation cache has to be cleared before cloning the fields $this->typedData = NULL;
// below so that the call to getTranslation() does not re-use the $definitions = $this->getFieldDefinitions();
// translation objects of the old entity but instead creates new
// translation objects from the newly cloned entity. Otherwise the newly // The translation cache has to be cleared before cloning the fields
// cloned field item lists would hold references to the old translation // below so that the call to getTranslation() does not re-use the
// objects in their $parent property after the call to setContext(). // translation objects of the old entity but instead creates new
$this->clearTranslationCache(); // translation objects from the newly cloned entity. Otherwise the newly
// cloned field item lists would hold references to the old translation
// Because the new translation objects that are created below are // objects in their $parent property after the call to setContext().
// themselves created by *cloning* the newly cloned entity we need to $this->clearTranslationCache();
// make sure that the references to property values are properly cloned
// before cloning the fields. Otherwise calling // Because the new translation objects that are created below are
// $items->getEntity()->isNew(), for example, would return the // themselves created by *cloning* the newly cloned entity we need to
// $enforceIsNew value of the old entity. // make sure that the references to property values are properly cloned
// before cloning the fields. Otherwise calling
// Ensure the translations array is actually cloned by overwriting the // $items->getEntity()->isNew(), for example, would return the
// original reference with one pointing to a copy of the array. // $enforceIsNew value of the old entity.
$translations = $this->translations;
$this->translations = &$translations; // Ensure the translations array is actually cloned by overwriting the
// original reference with one pointing to a copy of the array.
// Ensure that the following properties are actually cloned by $translations = $this->translations;
// overwriting the original references with ones pointing to copies of $this->translations = &$translations;
// them: enforceIsNew, newRevision, loadedRevisionId and fields.
$enforce_is_new = $this->enforceIsNew; // Ensure that the following properties are actually cloned by
$this->enforceIsNew = &$enforce_is_new; // overwriting the original references with ones pointing to copies of
// them: enforceIsNew, newRevision, loadedRevisionId and fields.
$new_revision = $this->newRevision; $enforce_is_new = $this->enforceIsNew;
$this->newRevision = &$new_revision; $this->enforceIsNew = &$enforce_is_new;
$original_revision_id = $this->loadedRevisionId; $new_revision = $this->newRevision;
$this->loadedRevisionId = &$original_revision_id; $this->newRevision = &$new_revision;
$fields = $this->fields; $original_revision_id = $this->loadedRevisionId;
$this->fields = &$fields; $this->loadedRevisionId = &$original_revision_id;
foreach ($this->fields as $name => $values) { $fields = $this->fields;
$this->fields[$name] = array(); $this->fields = &$fields;
// Untranslatable fields may have multiple references for the same field
// object keyed by language. To avoid creating different field objects foreach ($this->fields as $name => $values) {
// we retain just the original value, as references will be recreated $this->fields[$name] = array();
// later as needed. // Untranslatable fields may have multiple references for the same field
if (!$definitions[$name]->isTranslatable() && count($values) > 1) { // object keyed by language. To avoid creating different field objects
$values = array_intersect_key($values, array(LanguageInterface::LANGCODE_DEFAULT => TRUE)); // we retain just the original value, as references will be recreated
} // later as needed.
foreach ($values as $langcode => $items) { if (!$definitions[$name]->isTranslatable() && count($values) > 1) {
$this->fields[$name][$langcode] = clone $items; $values = array_intersect_key($values, array(LanguageInterface::LANGCODE_DEFAULT => TRUE));
$this->fields[$name][$langcode]->setContext($name, $this->getTranslation($langcode)->getTypedData()); }
} foreach ($values as $langcode => $items) {
$this->fields[$name][$langcode] = clone $items;
$this->fields[$name][$langcode]->setContext($name, $this->getTranslation($langcode)->getTypedData());
} }
} }
} }
......
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