You are here

public static function SocialLinkWidget::removeSubmit in Social Link Field 8

Submit callback to remove an item from the field UI multiple wrapper.

Parameters

array $form: The form structure where widgets are being attached to.

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

File

src/Plugin/Field/FieldWidget/SocialLinkWidget.php, line 301

Class

SocialLinkWidget
Plugin implementation of the 'open_hours' widget.

Namespace

Drupal\social_link_field\Plugin\Field\FieldWidget

Code

public static function removeSubmit(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();
  $delta = $button['#delta'];
  $address = array_slice($button['#array_parents'], 0, -4);
  $address_state = array_slice($button['#parents'], 0, -3);
  $parent_element = NestedArray::getValue($form, array_merge($address, [
    'widget',
  ]));
  $field_name = $parent_element['#field_name'];
  $parents = $parent_element['#field_parents'];
  $field_state = static::getWidgetState($parents, $field_name, $form_state);
  for ($i = $delta; $i <= $field_state['items_count']; $i++) {
    $old_element_address = array_merge($address, [
      'widget',
      $i + 1,
    ]);
    $old_element_state_address = array_merge($address_state, [
      $i + 1,
    ]);
    $new_element_state_address = array_merge($address_state, [
      $i,
    ]);
    $moving_element = NestedArray::getValue($form, $old_element_address);
    $moving_element_value = NestedArray::getValue($form_state
      ->getValues(), $old_element_state_address);
    $moving_element_input = NestedArray::getValue($form_state
      ->getUserInput(), $old_element_state_address);
    $moving_element_field = NestedArray::getValue($form_state
      ->get('field_storage'), array_merge([
      '#parents',
    ], $address));
    $moving_element['#parents'] = $new_element_state_address;
    $form_state
      ->setValueForElement($moving_element, $moving_element_value);
    $user_input = $form_state
      ->getUserInput();
    NestedArray::setValue($user_input, $moving_element['#parents'], $moving_element_input);
    $form_state
      ->setUserInput($user_input);
    NestedArray::setValue($form_state
      ->get('field_storage'), array_merge([
      '#parents',
    ], $moving_element['#parents']), $moving_element_field);
  }
  if ($field_state['items_count'] > 0) {
    $field_state['items_count']--;
  }
  static::setWidgetState($parents, $field_name, $form_state, $field_state);
  $form_state
    ->setRebuild();
}