You are here

public static function WebformMultiple::removeItemSubmit in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Element/WebformMultiple.php \Drupal\webform\Element\WebformMultiple::removeItemSubmit()

Webform submission handler for removing an item.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

File

src/Element/WebformMultiple.php, line 866

Class

WebformMultiple
Provides a webform element to assist in creation of multiple elements.

Namespace

Drupal\webform\Element

Code

public static function removeItemSubmit(array &$form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -4));
  $values = $element['items']['#value'];

  // Remove item.
  unset($values[$button['#row_index']]);
  $values = array_values($values);

  // Remove one item from the 'number of items'.
  $number_of_items_storage_key = static::getStorageKey($element, 'number_of_items');
  $number_of_items = $form_state
    ->get($number_of_items_storage_key);

  // Never allow the number of items to be less than #min_items.
  if ($number_of_items > $element['#min_items']) {
    $form_state
      ->set($number_of_items_storage_key, $number_of_items - 1);
  }

  // Reset values.
  $form_state
    ->setValueForElement($element['items'], $values);
  NestedArray::setValue($form_state
    ->getUserInput(), $element['items']['#parents'], $values);
  $action_key = static::getStorageKey($element, 'action');
  $form_state
    ->set($action_key, TRUE);

  // Rebuild the form.
  $form_state
    ->setRebuild();
}