Same name in other branches
  1. 8.x-1.x src/EntityClone/Content/ContentEntityCloneBase.php \Drupal\entity_clone\EntityClone\Content\ContentEntityCloneBase::cloneReferencedEntities()

Clones referenced entities.

Parameters

\Drupal\Core\Field\FieldItemListInterface $field: The field item.

\Drupal\Core\Field\FieldConfigInterface $field_definition: The field definition.

array $properties: All new properties to replace old.

array $already_cloned: List of all already cloned entities, used for circular references.

Return value

array Referenced entities.

1 call to ContentEntityCloneBase::cloneReferencedEntities()
ContentEntityCloneBase::cloneEntity in src/EntityClone/Content/ContentEntityCloneBase.php
Clone an entity.

File

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

Class

ContentEntityCloneBase
Class Content Entity Clone Base.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

protected function cloneReferencedEntities(FieldItemListInterface $field, FieldConfigInterface $field_definition, array $properties, array &$already_cloned) {
    $referenced_entities = [];
    foreach ($field as $value) {
        // Check if we're not dealing with an entity
        // that has been deleted in the meantime.
        if (!($referenced_entity = $value->get('entity')
            ->getTarget())) {
            continue;
        }
        
        /** @var \Drupal\Core\Entity\ContentEntityInterface $referenced_entity */
        $referenced_entity = $value->get('entity')
            ->getTarget()
            ->getValue();
        $child_properties = $this->getChildProperties($properties, $field_definition, $referenced_entity);
        if (!empty($child_properties['clone'])) {
            $cloned_reference = $referenced_entity->createDuplicate();
            
            /** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $entity_clone_handler */
            $entity_clone_handler = $this->entityTypeManager
                ->getHandler($referenced_entity->getEntityTypeId(), 'entity_clone');
            $entity_clone_handler->cloneEntity($referenced_entity, $cloned_reference, $child_properties['children'], $already_cloned);
            $referenced_entities[] = $cloned_reference;
        }
        elseif (!empty($child_properties['is_circular'])) {
            if (!empty($already_cloned[$referenced_entity->getEntityTypeId()][$referenced_entity->id()])) {
                $referenced_entities[] = $already_cloned[$referenced_entity->getEntityTypeId()][$referenced_entity->id()];
            }
            else {
                $referenced_entities[] = $referenced_entity;
            }
        }
        else {
            $referenced_entities[] = $referenced_entity;
        }
    }
    return $referenced_entities;
}