You are here

public function Replicator::replicateEntity in Replicate 8

Replicate a entity and save it.

Parameters

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

Return value

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

Throws

\Drupal\Component\Plugin\Exception\PluginNotFoundException Thrown if the entity type doesn't exist.

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException Thrown if the storage handler couldn't be loaded.

\Drupal\Core\Entity\EntityStorageException In case of failures, an exception is thrown.

1 call to Replicator::replicateEntity()
Replicator::replicateByEntityId in src/Replicator.php
Replicate a entity by entity type ID and entity ID and save it.

File

src/Replicator.php, line 96

Class

Replicator
Class Replicator. Manages the replication of an entity.

Namespace

Drupal\replicate

Code

public function replicateEntity(EntityInterface $entity) {
  if ($clone = $this
    ->cloneEntity($entity)) {
    $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId())
      ->save($clone);
    $event = new AfterSaveEvent($clone);
    $this->eventDispatcher
      ->dispatch(ReplicatorEvents::AFTER_SAVE, $event);
    return $clone;
  }
}