You are here

public function ContentEntityCloneBase::cloneEntity in Entity Clone 8

Clone an entity.

Parameters

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

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

array $properties: All new properties to replace old.

Return value

\Drupal\Core\Entity\EntityInterface The new saved entity.

Overrides EntityCloneInterface::cloneEntity

3 calls to ContentEntityCloneBase::cloneEntity()
FileEntityClone::cloneEntity in src/EntityClone/Content/FileEntityClone.php
Clone an entity.
TaxonomyTermEntityClone::cloneEntity in src/EntityClone/Content/TaxonomyTermEntityClone.php
Clone an entity.
UserEntityClone::cloneEntity in src/EntityClone/Content/UserEntityClone.php
Clone an entity.
3 methods override ContentEntityCloneBase::cloneEntity()
FileEntityClone::cloneEntity in src/EntityClone/Content/FileEntityClone.php
Clone an entity.
TaxonomyTermEntityClone::cloneEntity in src/EntityClone/Content/TaxonomyTermEntityClone.php
Clone an entity.
UserEntityClone::cloneEntity in src/EntityClone/Content/UserEntityClone.php
Clone an entity.

File

src/EntityClone/Content/ContentEntityCloneBase.php, line 85

Class

ContentEntityCloneBase
Class ContentEntityCloneBase.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

public function cloneEntity(EntityInterface $entity, EntityInterface $cloned_entity, array $properties = [], array &$already_cloned = []) {
  if (isset($properties['take_ownership']) && $properties['take_ownership'] === 1) {
    $cloned_entity
      ->setOwnerId($this->currentUser
      ->id());
  }

  // Clone referenced entities.
  $cloned_entity
    ->save();
  $already_cloned[$entity
    ->getEntityTypeId()][$entity
    ->id()] = $cloned_entity;
  if ($cloned_entity instanceof FieldableEntityInterface && $entity instanceof FieldableEntityInterface) {
    foreach ($cloned_entity
      ->getFieldDefinitions() as $field_id => $field_definition) {
      if ($this
        ->fieldIsClonable($field_definition)) {
        $field = $entity
          ->get($field_id);

        /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $value */
        if ($field
          ->count() > 0) {
          $cloned_entity
            ->set($field_id, $this
            ->cloneReferencedEntities($field, $field_definition, $properties, $already_cloned));
        }
      }
    }
  }
  $this
    ->setClonedEntityLabel($entity, $cloned_entity);

  // For now, check that the cloned entity has a 'setCreatedTime' method, and
  // if so, try to call it. This condition can be replaced with a more-robust
  // check whether $cloned_entity is an instance of
  // Drupal\Core\Entity\EntityCreatedInterface once
  // https://www.drupal.org/project/drupal/issues/2833378 lands.
  if (method_exists($cloned_entity, 'setCreatedTime')) {
    $cloned_entity
      ->setCreatedTime($this->timeService
      ->getRequestTime());
  }
  $cloned_entity
    ->save();
  return $cloned_entity;
}