You are here

public static function InlineEntityFormComplex::submitConfirmRemove in Inline Entity Form 8

Remove form submit callback.

The row is identified by #ief_row_delta stored on the triggering element. This isn't an #element_validate callback to avoid processing the remove form when the main form is submitted.

Parameters

$form: The complete parent form.

$form_state: The form state of the parent form.

File

src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php, line 875

Class

InlineEntityFormComplex
Complex inline widget.

Namespace

Drupal\inline_entity_form\Plugin\Field\FieldWidget

Code

public static function submitConfirmRemove($form, FormStateInterface $form_state) {
  $element = inline_entity_form_get_element($form, $form_state);
  $remove_button = $form_state
    ->getTriggeringElement();
  $delta = $remove_button['#ief_row_delta'];

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = $element['entities'][$delta]['form']['#entity'];
  $entity_id = $entity
    ->id();
  $form_values = NestedArray::getValue($form_state
    ->getValues(), $element['entities'][$delta]['form']['#parents']);
  $form_state
    ->setRebuild();
  $widget_state = $form_state
    ->get([
    'inline_entity_form',
    $element['#ief_id'],
  ]);

  // This entity hasn't been saved yet, we can just unlink it.
  if (empty($entity_id) || $remove_button['#allow_existing'] && empty($form_values['delete'])) {
    unset($widget_state['entities'][$delta]);
  }
  else {
    $widget_state['delete'][] = $entity;
    unset($widget_state['entities'][$delta]);
  }
  $form_state
    ->set([
    'inline_entity_form',
    $element['#ief_id'],
  ], $widget_state);
}