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;

    // Ensure the enforceIsNew property is actually cloned by overwriting the
    // original reference with one pointing to a copy of it.
    $enforce_is_new = $this->enforceIsNew;
    $this->enforceIsNew = &$enforce_is_new;
  }
}
doc_Drupal
2016-10-29 08:57:02
Comments
Leave a Comment

Please login to continue.