You are here

protected function ContentEntityCloneFormBase::getRecursiveFormElement in Entity Clone 8

Get the recursive form element.

Parameters

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

string $field_id: The field ID.

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

array $discovered_entities: List of all entities already discovered.

Return value

array The form element for a recursive clone.

1 call to ContentEntityCloneFormBase::getRecursiveFormElement()
ContentEntityCloneFormBase::formElement in src/EntityClone/Content/ContentEntityCloneFormBase.php
Get all specific form element.

File

src/EntityClone/Content/ContentEntityCloneFormBase.php, line 133

Class

ContentEntityCloneFormBase
Class ContentEntityCloneFormBase.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

protected function getRecursiveFormElement(FieldConfigInterface $field_definition, $field_id, FieldItemListInterface $field, array &$discovered_entities) {
  $form_element = [
    '#tree' => TRUE,
  ];
  $fieldset_access = !$this->entityCloneSettingsManager
    ->getHiddenValue($field_definition
    ->getFieldStorageDefinition()
    ->getSetting('target_type'));
  $form_element[$field_definition
    ->id()] = [
    '#type' => 'fieldset',
    '#title' => $this->translationManager
      ->translate('Entities referenced by field <em>@label (@field_id)</em>.', [
      '@label' => $field_definition
        ->label(),
      '@field_id' => $field_id,
    ]),
    '#access' => $fieldset_access,
    '#description_should_be_shown' => $fieldset_access,
  ];
  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();
    if (isset($discovered_entities[$referenced_entity
      ->getEntityTypeId()]) && array_key_exists($referenced_entity
      ->id(), $discovered_entities[$referenced_entity
      ->getEntityTypeId()])) {
      $form_element[$field_definition
        ->id()]['references'][$referenced_entity
        ->id()]['is_circular'] = [
        '#type' => 'hidden',
        '#value' => TRUE,
      ];
      $form_element[$field_definition
        ->id()]['references'][$referenced_entity
        ->id()]['circular'] = [
        '#type' => 'item',
        '#markup' => $this->translationManager
          ->translate('Circular reference detected'),
      ];
    }
    else {
      $form_element[$field_definition
        ->id()]['references'][$referenced_entity
        ->id()]['clone'] = [
        '#type' => 'checkbox',
        '#title' => $this->translationManager
          ->translate('Clone entity <strong>ID:</strong> <em>@entity_id</em>, <strong>Type:</strong> <em>@entity_type - @bundle</em>, <strong>Label:</strong> <em>@entity_label</em>', [
          '@entity_id' => $referenced_entity
            ->id(),
          '@entity_type' => $referenced_entity
            ->getEntityTypeId(),
          '@bundle' => $referenced_entity
            ->bundle(),
          '@entity_label' => $referenced_entity
            ->label(),
        ]),
        '#default_value' => $this->entityCloneSettingsManager
          ->getDefaultValue($referenced_entity
          ->getEntityTypeId()),
        '#access' => $referenced_entity
          ->access('view label'),
      ];
      if ($this->entityCloneSettingsManager
        ->getDisableValue($referenced_entity
        ->getEntityTypeId())) {
        $form_element[$field_definition
          ->id()]['references'][$referenced_entity
          ->id()]['clone']['#attributes'] = [
          'disabled' => TRUE,
        ];
        $form_element[$field_definition
          ->id()]['references'][$referenced_entity
          ->id()]['clone']['#value'] = $form_element[$field_definition
          ->id()]['references'][$referenced_entity
          ->id()]['clone']['#default_value'];
      }
      $form_element[$field_definition
        ->id()]['references'][$referenced_entity
        ->id()]['target_entity_type_id'] = [
        '#type' => 'hidden',
        '#value' => $referenced_entity
          ->getEntityTypeId(),
      ];
      $form_element[$field_definition
        ->id()]['references'][$referenced_entity
        ->id()]['target_bundle'] = [
        '#type' => 'hidden',
        '#value' => $referenced_entity
          ->bundle(),
      ];
      if ($referenced_entity instanceof ContentEntityInterface) {
        $form_element[$field_definition
          ->id()]['references'][$referenced_entity
          ->id()]['children'] = $this
          ->getChildren($referenced_entity, $discovered_entities);
      }
    }
  }
  return $form_element;
}