/**
  * 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.
  *
  * @param $form
  *   The complete parent form.
  * @param $form_state
  *   The form state of the parent form.
  */
 public static function submitConfirmRemove($form, FormStateInterface $form_state)
 {
     $element = inline_entity_form_get_element($form, $form_state);
     $delta = $form_state->getTriggeringElement()['#ief_row_delta'];
     /** @var \Drupal\Core\Field\FieldDefinitionInterface $instance */
     $instance = $form_state->get(['inline_entity_form', $element['#ief_id'], 'instance']);
     /** @var \Drupal\Core\Entity\EntityInterface $entity */
     $entity = $element['entities'][$delta]['form']['#entity'];
     $entity_id = $entity->id();
     $widget = \Drupal::entityManager()->getStorage('entity_form_display')->load($instance->getTargetEntityTypeId() . '.' . $instance->getTargetBundle() . '.default')->getComponent($instance->getName());
     $form_values = NestedArray::getValue($form_state->getValues(), $element['entities'][$delta]['form']['#parents']);
     $form_state->setRebuild();
     // This entity hasn't been saved yet, we can just unlink it.
     if (empty($entity_id) || $widget['settings']['allow_existing'] && empty($form_values['delete'])) {
         $form_state->set(['inline_entity_form', $element['#ief_id'], 'entities', $delta], NULL);
     } else {
         $delete = $form_state->get(['inline_entity_form', $element['#ief_id'], 'delete']);
         $delete['delete'][] = $entity_id;
         $form_state->set(['inline_entity_form', $element['#ief_id'], 'delete'], $delete);
         $form_state->set(['inline_entity_form', $element['#ief_id'], 'entities', $delta], NULL);
     }
 }
Exemplo n.º 2
0
 /**
  * Button #submit callback:  Closes all open child forms in the IEF widget.
  *
  * Used to ensure that forms in nested IEF widgets are properly closed
  * when a parent IEF's form gets submitted or cancelled.
  *
  * @param $form
  *   The IEF Form element.
  * @param FormStateInterface $form_state
  *   The form state of the parent form.
  */
 public static function closeChildForms($form, FormStateInterface &$form_state) {
   $element = inline_entity_form_get_element($form, $form_state);
   inline_entity_form_close_all_forms($element, $form_state);
 }