diff --git a/inline_entity_form.module b/inline_entity_form.module index 48adc49..bb433cf 100644 --- a/inline_entity_form.module +++ b/inline_entity_form.module @@ -269,12 +269,14 @@ function inline_entity_form_close_all_forms($elements, FormStateInterface $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']); - if (isset($elements['#ief_row_delta']) && isset($entities[$elements['#ief_row_delta']])) { - $entities[$elements['#ief_row_delta']]['form'] = NULL; - } - else { - foreach ($entities as $key => $value) { - $entities[$key]['form'] = NULL; + if (!empty($entities)) { + if (isset($elements['#ief_row_delta']) && isset($entities[$elements['#ief_row_delta']])) { + $entities[$elements['#ief_row_delta']]['form'] = NULL; + } + else { + foreach ($entities as $key => $value) { + $entities[$key]['form'] = NULL; + } } } $form_state->set(['inline_entity_form', $ief_id, 'entities'], $entities); diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 82b3fd6..38aaee8 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -895,12 +895,14 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF public static function closeChildForms($form, FormStateInterface &$form_state) { $element = inline_entity_form_get_element($form, $form_state); $triggering_element = $form_state->getTriggeringElement(); - if (isset($triggering_element['#ief_row_delta']) && isset($element['entities'][$triggering_element['#ief_row_delta']])) { - inline_entity_form_close_all_forms($element['entities'][$triggering_element['#ief_row_delta']], $form_state); - } - else { + if (!preg_match('~^ief-add-cancel~i', $triggering_element['#name'])) { + $array_parents = array_reverse($triggering_element['#array_parents']); + while (current($array_parents) != 'inline_entity_form') { + array_shift($array_parents); + } + $array_parents = array_reverse($array_parents); + $element = NestedArray::getValue($form, $array_parents) ?: $element; inline_entity_form_close_all_forms($element, $form_state); } } - } diff --git a/src/Tests/ComplexSimpleWidgetTest.php b/src/Tests/ComplexSimpleWidgetTest.php index 115a879..8b70ae3 100644 --- a/src/Tests/ComplexSimpleWidgetTest.php +++ b/src/Tests/ComplexSimpleWidgetTest.php @@ -108,4 +108,32 @@ class ComplexSimpleWidgetTest extends InlineEntityFormTestBase { } } + /** + * Test that the scope of a Cancel button is isolated to the form that it is + * associated with. + */ + public function testIsolatedCancel() { + $this->drupalGet('node/add/ief_complex_simple'); + // Setup for the test, create two entities + $outer_title_field = 'ief_complex_outer[form][inline_entity_form][title][0][value]'; + $inner_title_field = 'ief_complex_outer[form][inline_entity_form][single][0][inline_entity_form][title][0][value]'; + for ($i = 0; $i < 2; $i++) { + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-ief-complex-outer-actions-ief-add"]')); + $edit[$outer_title_field] = $this->randomMachineName(8); + $edit[$inner_title_field] = $this->randomMachineName(8); + $create_outer_button_selector = '//input[@type="submit" and @value="Create node" and @data-drupal-selector="edit-ief-complex-outer-form-inline-entity-form-actions-ief-add-save"]'; + $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName($create_outer_button_selector)); + // After it's been created, open the form + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Edit" and @data-drupal-selector="edit-ief-complex-outer-entities-' . $i . '-actions-ief-entity-edit"]')); + } + // At this point, both forms are open for the entities we created. Cancel + // the first, we expect the second to remain open. + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Cancel" and @data-drupal-selector="edit-ief-complex-outer-form-inline-entity-form-entities-0-form-actions-ief-edit-cancel"]')); + $this->assertFieldByName('ief_complex_outer[form][inline_entity_form][entities][1][form][title][0][value]', '', 'Second inline edit form remained open when the first was closed.'); + // Now lets open a Create form, and Cancel it, we should be left with the + // same second inline entity edit form still open. + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-ief-complex-outer-actions-ief-add"]')); + $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Cancel" and @data-drupal-selector="edit-ief-complex-outer-form-inline-entity-form-actions-ief-add-cancel"]')); + $this->assertFieldByName('ief_complex_outer[form][inline_entity_form][entities][1][form][title][0][value]', '', 'Second inline edit form remained open when the create inline entity form was closed.'); + } }