public function ContentEntityBase::createDuplicate

public ContentEntityBase::createDuplicate()

Creates a duplicate of the entity.

Return value

static A clone of $this with all identifiers unset, so saving it inserts a new entity into the storage system.

Overrides Entity::createDuplicate

File

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

Class

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

Namespace

Drupal\Core\Entity

Code

public function createDuplicate() {
  if ($this->translations[$this->activeLangcode]['status'] == static::TRANSLATION_REMOVED) {
    throw new \InvalidArgumentException("The entity object refers to a removed translation ({$this->activeLangcode}) and cannot be manipulated.");
  }

  $duplicate = clone $this;
  $entity_type = $this->getEntityType();
  $duplicate->{$entity_type->getKey('id')}->value = NULL;
  $duplicate->enforceIsNew();

  // Check if the entity type supports UUIDs and generate a new one if so.
  if ($entity_type->hasKey('uuid')) {
    $duplicate->{$entity_type->getKey('uuid')}->value = $this->uuidGenerator()->generate();
  }

  // Check whether the entity type supports revisions and initialize it if so.
  if ($entity_type->isRevisionable()) {
    $duplicate->{$entity_type->getKey('revision')}->value = NULL;
  }

  return $duplicate;
}

© 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::createDuplicate/8.1.x