You are here

public static function EntityqueueDragtableWidget::removeSubmit in Entityqueue 8

Submission handler for the "Remove" button.

File

src/Plugin/Field/FieldWidget/EntityqueueDragtableWidget.php, line 177

Class

EntityqueueDragtableWidget
Plugin implementation of the 'entityqueue_dragtable' widget.

Namespace

Drupal\entityqueue\Plugin\Field\FieldWidget

Code

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

  // Go two levels up in the form, to the widgets container.
  $element = NestedArray::getValue($form, array_slice($button['#array_parents'], 0, -2));
  $field_name = $element['#field_name'];
  $parents = $element['#field_parents'];

  // Set a NULL target ID for the removed element.
  $form_state
    ->setValueForElement($element[$button['#delta']]['target_id'], NULL);

  // Update the field item list values.
  $items = $form_state
    ->getFormObject()
    ->getEntity()
    ->get($field_name);
  $widget = $form_state
    ->getFormObject()
    ->getFormDisplay($form_state)
    ->getRenderer($field_name);
  $widget
    ->extractFormValues($items, $form, $form_state);

  // Remove unneeded properties.
  foreach ($items as $item) {
    unset($item->_remove);
  }

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