public function ContentEntityBase::__clone

public ContentEntityBase::__clone()

Magic method: Implements a deep clone.

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1007

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

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) {
    // The translation is a different object, and needs its own TypedData
    // adapter object.
    $this->typedData = NULL;
    $definitions = $this->getFieldDefinitions();
    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());
      }
    }

    // Ensure the translations array is actually cloned by overwriting the
    // original reference with one pointing to a copy of the array.
    $this->clearTranslationCache();
    $translations = $this->translations;
    $this->translations = &$translations;
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!ContentEntityBase.php/function/ContentEntityBase::__clone/8.1.x