You are here

public function Replicator::cloneEntity in Replicate 8

Clone a entity without saving.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

Return value

\Drupal\Core\Entity\EntityInterface|null The cloned entity.

2 calls to Replicator::cloneEntity()
Replicator::cloneByEntityId in src/Replicator.php
Clone a entity by entity type ID and entity ID without saving.
Replicator::replicateEntity in src/Replicator.php
Replicate a entity and save it.

File

src/Replicator.php, line 136

Class

Replicator
Class Replicator. Manages the replication of an entity.

Namespace

Drupal\replicate

Code

public function cloneEntity(EntityInterface $entity) {
  if ($clone = $entity
    ->createDuplicate()) {
    $event = new ReplicateEntityEvent($entity);
    $this->eventDispatcher
      ->dispatch(ReplicatorEvents::replicateEntityEvent($entity
      ->getEntityTypeId()), $event);
    if ($clone instanceof FieldableEntityInterface) {

      /** @var FieldableEntityInterface $clone */
      $this
        ->dispatchEventCloneEntityFields($clone);
    }
    $event = new ReplicateAlterEvent($clone, $entity);
    $this->eventDispatcher
      ->dispatch(ReplicatorEvents::REPLICATE_ALTER, $event);
    return $clone;
  }
}