You are here

protected function ConfigWithFieldEntityClone::cloneFields in Entity Clone 8

Clone all fields. Each field re-use existing field storage.

Parameters

string $entity_id: The base entity ID.

string $cloned_entity_id: The cloned entity ID.

string $bundle_of: The bundle of the cloned entity.

1 call to ConfigWithFieldEntityClone::cloneFields()
ConfigWithFieldEntityClone::cloneEntity in src/EntityClone/Config/ConfigWithFieldEntityClone.php
Clone an entity.

File

src/EntityClone/Config/ConfigWithFieldEntityClone.php, line 48

Class

ConfigWithFieldEntityClone
Class ContentEntityCloneBase.

Namespace

Drupal\entity_clone\EntityClone\Config

Code

protected function cloneFields($entity_id, $cloned_entity_id, $bundle_of) {

  /** @var \Drupal\Core\Entity\EntityFieldManager $field_manager */
  $field_manager = \Drupal::service('entity_field.manager');
  $fields = $field_manager
    ->getFieldDefinitions($bundle_of, $entity_id);
  foreach ($fields as $field_definition) {
    if ($field_definition instanceof FieldConfigInterface) {
      if ($this->entityTypeManager
        ->hasHandler($this->entityTypeManager
        ->getDefinition($field_definition
        ->getEntityTypeId())
        ->id(), 'entity_clone')) {

        /** @var \Drupal\entity_clone\EntityClone\EntityCloneInterface $field_config_clone_handler */
        $field_config_clone_handler = $this->entityTypeManager
          ->getHandler($this->entityTypeManager
          ->getDefinition($field_definition
          ->getEntityTypeId())
          ->id(), 'entity_clone');
        $field_config_properties = [
          'id' => $field_definition
            ->getName(),
          'label' => $field_definition
            ->label(),
          'skip_storage' => TRUE,
        ];
        $cloned_field_definition = $field_definition
          ->createDuplicate();
        $cloned_field_definition
          ->set('bundle', $cloned_entity_id);
        $field_config_clone_handler
          ->cloneEntity($field_definition, $cloned_field_definition, $field_config_properties);
      }
    }
  }
}