From b8b13b47fd969980864b7698fff1c9f272a1c764 Mon Sep 17 00:00:00 2001
From: xjm <xjm@65776.no-reply.drupal.org>
Date: Thu, 23 Feb 2017 00:27:02 +0000
Subject: [PATCH] Revert "Issue #2854926 by xjm, borisson_, boaloysius: Remove
 unneeded control structures in ContentEntityBase"

This reverts commit ccc65da733005958bf39cb1f64cfdde6e35c3ebe.
---
 .../Drupal/Core/Entity/ContentEntityBase.php  | 121 +++++++++---------
 1 file changed, 60 insertions(+), 61 deletions(-)

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