Same name and namespace in other branches
  1. 2.x src/EntityClone/Content/ContentEntityCloneBase.php \Drupal\entity_clone\EntityClone\Content\ContentEntityCloneBase::cloneEntity()

Overrides EntityCloneInterface::cloneEntity

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 94

Class

ContentEntityCloneBase
Class Content Entity Clone Base.

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.
    $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, $properties);
    $this->setCreatedAndChangedDates($cloned_entity);
    if ($this->hasTranslatableModerationState($cloned_entity)) {
        // If we are using moderation state, ensure that each translation gets
        // the same moderation state BEFORE we save so that upon save, each
        // translation gets its publishing status updated according to the
        // moderation state. After the entity is saved, we kick in the creation
        // of translations of created moderation state entity.
        foreach ($cloned_entity->getTranslationLanguages(TRUE) as $language) {
            $translation = $cloned_entity->getTranslation($language->getId());
            $translation->set('moderation_state', $cloned_entity->get('moderation_state')->value);
        }
    }
    $cloned_entity->save();
    // If we are using content moderation, make sure the moderation state
    // entity gets translated to reflect the available translations on the
    // source entity. Thus, we call this after the save because we need the
    // original moderation state entity to have been created.
    if ($this->hasTranslatableModerationState($cloned_entity)) {
        $this->setTranslationModerationState($entity, $cloned_entity);
    }
    return $cloned_entity;
}