W3cubDocs

/Drupal 8

public function Entity::createDuplicate

public Entity::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 EntityInterface::createDuplicate

File

core/lib/Drupal/Core/Entity/Entity.php, line 379

Class

Entity
Defines a base entity class.

Namespace

Drupal\Core\Entity

Code

public function createDuplicate() {
  $duplicate = clone $this;
  $entity_type = $this->getEntityType();
  // Reset the entity ID and indicate that this is a new entity.
  $duplicate->{$entity_type->getKey('id')} = 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')} = $this->uuidGenerator()->generate();
  }
  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!Entity.php/function/Entity::createDuplicate/8.1.x