You are here

public static function ComponentSectionForm::removeItemSubmit in Module Builder 8.3

Submission handler for the "Remove item" buttons.

File

src/Form/ComponentSectionForm.php, line 643

Class

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

Namespace

Drupal\module_builder\Form

Code

public static function removeItemSubmit(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();

  // Get the data item, using the address set in the button.
  $data = $form_state
    ->get('data');
  $multiple_data_item = $data
    ->getItem($button['#data_address']);

  // dsm($multiple_data_item);
  // We could remove any of the items here, but the problem is then that
  // FormAPI appears to put the currently entered values back into the form
  // elements whose deltas have closed the gap, which makes it look like it
  // was the last one that was removed anyway.
  $last_delta = count($multiple_data_item) - 1;
  unset($multiple_data_item[$last_delta]);
  $form_state
    ->set('data', $data);
  $form_state
    ->setRebuild();
}