You are here

public function ParagraphCloneForm::form in Paragraphs Edit 8.2

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

src/Form/ParagraphCloneForm.php, line 44

Class

ParagraphCloneForm
ParagraphCloneForm class.

Namespace

Drupal\paragraphs_edit\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $wrapper_id = Html::getUniqueId('paragraphs-edit-clone');
  $form['paragraphs_edit'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Clone to'),
    '#id' => $wrapper_id,
    '#tree' => TRUE,
  ];
  $potential_destinations = $this
    ->getPotentialCloneDestinations($this->entity
    ->bundle());
  $entity_type_labels = $this->entityManager
    ->getEntityTypeLabels();
  $entity_types = array_intersect_key($entity_type_labels, $potential_destinations);
  $form['paragraphs_edit']['entity_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type'),
    '#options' => $entity_types,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::paragraphEditChangeAjax',
      'wrapper' => $wrapper_id,
    ],
  ];
  $bundles = [];
  $entity_type = $form_state
    ->getValue([
    'paragraphs_edit',
    'entity_type',
  ], NULL);
  if (!empty($entity_type)) {
    $bundles = $potential_destinations[$entity_type]['bundles'];
  }
  $form['paragraphs_edit']['bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Bundle'),
    '#options' => $bundles,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::paragraphEditChangeAjax',
      'wrapper' => $wrapper_id,
    ],
    '#disabled' => empty($entity_type),
  ];
  $selection_settings = [];
  $bundle = $form_state
    ->getValue([
    'paragraphs_edit',
    'bundle',
  ], NULL);
  if (!empty($bundle)) {
    $selection_settings['target_bundles'] = [
      $bundle,
    ];
  }
  if (!$entity_type) {
    $entity_type = array_keys($potential_destinations)[0];
  }
  $form['paragraphs_edit']['parent'] = [
    '#type' => 'entity_autocomplete',
    '#title' => $this
      ->t('Parent'),
    '#target_type' => $entity_type,
    '#selection_handler' => 'default',
    '#selection_settings' => $selection_settings,
    '#required' => TRUE,
    '#disabled' => empty($bundle),
  ];
  $form['paragraphs_edit']['field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Field'),
    '#options' => !empty($bundle) ? $potential_destinations[$entity_type]['fields'][$bundle] : [],
    '#required' => TRUE,
    '#disabled' => empty($bundle),
  ];
  if (count($form['paragraphs_edit']['field']['#options']) == 1) {
    $form['paragraphs_edit']['field']['#default_value'] = key($form['paragraphs_edit']['field']['#options']);
  }
  $form = parent::form($form, $form_state);
  $form['#title'] = $this
    ->t('Clone @lineage', [
    '@lineage' => $this
      ->lineageInspector()
      ->getLineageString($this->originalEntity),
  ]);
  return $form;
}