You are here

public static function BatOptionsCombined::removeItemSubmit in Booking and Availability Management Tools for Drupal 8

File

modules/bat_options/src/Plugin/Field/FieldWidget/BatOptionsCombined.php, line 227
Contains \Drupal\bat_options\Plugin\Field\FieldWidget\BatOptionsCombined.

Class

BatOptionsCombined
Plugin annotation @FieldWidget( id = "bat_options_combined", label = @Translation("Combined text field'"), field_types = { "bat_options" } )

Namespace

Drupal\bat_options\Plugin\Field\FieldWidget

Code

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

  // Go one level up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -1));
  $container_element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
  $field_name = $container_element['#field_name'];
  $field_parents = $element['#field_parents'];
  $delta = $element['#delta'];
  $field_values =& $form_state
    ->getValue($container_element['#parents']);
  $field_input =& NestedArray::getValue($form_state
    ->getUserInput(), $container_element['#parents']);
  $field_state = static::getWidgetState($field_parents, $field_name, $form_state);
  for ($i = $delta; $i < $field_state['items_count']; $i++) {
    $field_values[$i] = $field_values[$i + 1];
    $field_input[$i] = $field_input[$i + 1];
  }
  unset($field_values[$field_state['items_count']]);
  unset($field_input[$field_state['items_count']]);

  // Increment the items count.
  $field_state['items_count']--;
  static::setWidgetState($field_parents, $field_name, $form_state, $field_state);
  $form_state
    ->setRebuild();
}