You are here

public function ParagraphCloneForm::form in Paragraphs Edit 8

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/ParagraphCloneForm.php, line 47

Class

ParagraphCloneForm

Namespace

Drupal\paragraphs_edit

Code

public function form(array $form, FormStateInterface $form_state) {
  $route_match = $this
    ->getRouteMatch();

  /** @var \Drupal\node\NodeInterface $node */
  $node = $route_match
    ->getParameter('node');
  $field_name = $route_match
    ->getParameter('field');
  $field = $node
    ->get($field_name);
  $field_definition = $field
    ->getFieldDefinition();
  $field_label = $field_definition
    ->getLabel();
  $delta = $route_match
    ->getParameter('delta');
  $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());
  $form['paragraphs_edit']['bundle'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Type'),
    '#options' => $potential_destinations['bundles'],
    '#required' => TRUE,
    '#ajax' => [
      'callback' => '::paragraphEditChangeAjax',
      'wrapper' => $wrapper_id,
    ],
  ];
  $selection_settings = [];
  $bundle = $form_state
    ->getValue([
    'paragraphs_edit',
    'bundle',
  ], NULL);
  if (!empty($bundle)) {
    $selection_settings['target_bundles'] = [
      $bundle,
    ];
  }
  $form['paragraphs_edit']['parent'] = [
    '#type' => 'entity_autocomplete',
    '#title' => $this
      ->t('Parent'),
    '#target_type' => 'node',
    '#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['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 #@delta of @field of %label', [
    '@delta' => $delta,
    '@field' => $field_label,
    '%label' => $node
      ->label(),
  ]);
  return $form;
}