You are here

protected function ComponentSectionForm::buildMultipleDeltaFormElement in Module Builder 8.3

Builds a multi-valued form element.

Helper for buildFormElement().

Note that buildFormElement() is responsible for some attributes of the element.

1 call to ComponentSectionForm::buildMultipleDeltaFormElement()
ComponentSectionForm::buildFormElement in src/Form/ComponentSectionForm.php
Builds the form element for a data item.

File

src/Form/ComponentSectionForm.php, line 378

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

protected function buildMultipleDeltaFormElement(&$form, FormStateInterface $form_state, DataItem $data) {

  // Set up a wrapper for AJAX.
  $wrapper_id = Html::getId($data
    ->getAddress() . '-add-more-wrapper');

  // Use 'details' rather than 'container' so there's a visual indicator
  // of the multi-valued property.
  $element = [
    '#type' => 'details',
    '#title' => $data
      ->getLabel(),
    '#open' => TRUE,
    '#attributes' => [
      'id' => $wrapper_id,
    ],
  ];
  foreach ($data as $delta => $delta_item) {
    $this
      ->buildFormElement($element, $form_state, $delta_item);

    // Set the label on each delta item to differentiate it from the overall
    // element label.
    $element[$delta]['#title'] = $delta_item
      ->getLabel();

    // Doesn't work; see removeItemSubmit().
    // $element[':' . $delta_item->getName() . '_remove_button'] = [
    //   '#type' => 'submit',
    //   // Needs to be full address for uniquess in the whole form.
    //   '#name' => $data->getAddress() . '_remove_item',
    //   '#value' => t('Remove item'),
    //   // Hack?
    //   '#input' => $delta,
    //   '#limit_validation_errors' => [],
    //   '#submit' => ['::removeItemSubmit'],
    //   '#ajax' => [
    //     'callback' => '::itemButtonAjax',
    //     'wrapper' => $wrapper_id,
    //     'effect' => 'fade',
    //   ],
    // ];
  }
  if ($data
    ->mayAddItem()) {
    if (count($data)) {
      $button_label = $this
        ->t('Add another @label item', [
        '@label' => $data
          ->getLabel(),
      ]);
    }
    else {
      $button_label = $this
        ->t('Add a @label item', [
        '@label' => $data
          ->getLabel(),
      ]);
    }
    $element[':add_button'] = [
      '#type' => 'submit',
      // This allows FormAPI to figure out which button is the triggering
      // element. The name must be unique across all buttons in the form,
      // otherwise, the first matching name will be taken by FormAPI as being
      // the button that was clicked, with unexpected results.
      // See \Drupal\Core\Form\FormBuilder::elementTriggeredScriptedSubmission().
      '#name' => $data
        ->getAddress() . '_add_more',
      '#value' => $button_label,
      '#limit_validation_errors' => [],
      '#submit' => [
        '::addItemSubmit',
      ],
      '#data_address' => $data
        ->getAddress(),
      '#ajax' => [
        'callback' => '::itemButtonAjax',
        'wrapper' => $wrapper_id,
        'effect' => 'fade',
      ],
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];
  }
  if (count($data)) {
    $element[':remove_button'] = [
      '#type' => 'submit',
      // Needs to be full address for uniquess in the whole form.
      '#name' => $data
        ->getAddress() . '_remove_item',
      '#value' => $this
        ->t('Remove last item'),
      '#limit_validation_errors' => [],
      '#submit' => [
        '::removeItemSubmit',
      ],
      '#data_address' => $data
        ->getAddress(),
      '#ajax' => [
        'callback' => '::itemButtonAjax',
        'wrapper' => $wrapper_id,
        'effect' => 'fade',
      ],
      '#prefix' => '<div>',
      '#suffix' => '</div>',
    ];
  }
  return $element;
}