You are here

public function ContentEntityCloneFormBase::formElement in Entity Clone 8

Get all specific form element.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

bool $parent: Is the parent form element.

Return value

array The form elements.

Overrides EntityCloneFormInterface::formElement

File

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

Class

ContentEntityCloneFormBase
Class ContentEntityCloneFormBase.

Namespace

Drupal\entity_clone\EntityClone\Content

Code

public function formElement(EntityInterface $entity, $parent = TRUE, &$discovered_entities = []) {
  $form = [
    'recursive' => [],
  ];
  if ($entity instanceof FieldableEntityInterface) {
    $discovered_entities[$entity
      ->getEntityTypeId()][$entity
      ->id()] = $entity;
    foreach ($entity
      ->getFieldDefinitions() as $field_id => $field_definition) {
      if ($field_definition instanceof FieldConfigInterface && in_array($field_definition
        ->getType(), [
        'entity_reference',
        'entity_reference_revisions',
      ], TRUE)) {
        $field = $entity
          ->get($field_id);

        /** @var \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem $value */
        if ($field
          ->count() > 0 && $this->entityTypeManager
          ->getStorage($field
          ->getSetting('target_type')) instanceof ContentEntityStorageInterface) {
          $form['recursive'] = array_merge($form['recursive'], $this
            ->getRecursiveFormElement($field_definition, $field_id, $field, $discovered_entities));
        }
      }
    }
    if ($parent) {
      $form = array_merge([
        'description' => [
          '#markup' => $this
            ->getFormDescription($form, $entity),
          '#access' => TRUE,
        ],
      ], $form);
    }
  }
  return $form;
}