You are here

public function ContentEntityBase::__clone in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::__clone()

Magic method: Implements a deep clone.

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1010
Contains \Drupal\Core\Entity\ContentEntityBase.

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;
  }
}