You are here

protected function ContentEntityCloneBase::cloneReferencedEntities in Entity Clone 8

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 171

Class

ContentEntityCloneBase
Class ContentEntityCloneBase.

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;
}