You are here

function inline_entity_form_close_all_forms in Inline Entity Form 8

Same name and namespace in other branches
  1. 7 inline_entity_form.module \inline_entity_form_close_all_forms()

Closes all open IEF forms.

Recurses and closes open forms in nested IEF widgets as well.

Parameters

$elements: An array of form elements containing entity forms.

$form_state: The form state of the parent form.

1 call to inline_entity_form_close_all_forms()
InlineEntityFormComplex::closeChildForms in src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
Button #submit callback: Closes all open child forms in the IEF widget.

File

./inline_entity_form.module, line 268
Provides a widget for inline management (creation, modification, removal) of referenced entities. The primary use case is the parent -> children one (for example, order -> line items), where the child entities are never managed outside the…

Code

function inline_entity_form_close_all_forms($elements, FormStateInterface $form_state) {

  // Recurse through all children.
  foreach (Element::children($elements) as $key) {
    if (!empty($elements[$key])) {
      inline_entity_form_close_all_forms($elements[$key], $form_state);
    }
  }
  if (!empty($elements['#ief_id'])) {
    $ief_id = $elements['#ief_id'];

    // Close the main form.
    $form_state
      ->set([
      'inline_entity_form',
      $ief_id,
      'form',
    ], NULL);

    // Close the row forms.
    $entities = $form_state
      ->get([
      'inline_entity_form',
      $ief_id,
      'entities',
    ]);
    foreach ($entities as $key => $value) {
      $entities[$key]['form'] = NULL;
    }
    $form_state
      ->set([
      'inline_entity_form',
      $ief_id,
      'entities',
    ], $entities);
  }
}