diff --git a/config/schema/inline_entity_form.schema.yml b/config/schema/inline_entity_form.schema.yml index a54733d4e3f99ff7fccc843621ffb1f9a23a3f38..43b5d6525f11a5a7c03c07f34e8f238aa0853642 100644 --- a/config/schema/inline_entity_form.schema.yml +++ b/config/schema/inline_entity_form.schema.yml @@ -69,6 +69,9 @@ field.widget.settings.inline_entity_form_complex: allow_existing: type: boolean label: "Allow existing" + allow_edit: + type: boolean + label: "Allow edit" match_operator: type: string label: "Match operator" diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 229cef737fdbd916fc78178b26733e5ecb15fba2..57a0ae1ec6b48a430288e44717b6c386d24966f3 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -119,6 +119,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $defaults += [ 'allow_new' => TRUE, 'allow_existing' => FALSE, + 'allow_edit' => TRUE, 'removed_reference' => self::REMOVED_OPTIONAL, 'match_operator' => 'CONTAINS', 'allow_duplicate' => FALSE, @@ -153,6 +154,11 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF '#title' => $this->t('Allow users to add existing @label.', ['@label' => $labels['plural']]), '#default_value' => $this->getSetting('allow_existing'), ]; + $element['allow_edit'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Allow users to edit existing @label.', ['@label' => $labels['plural']]), + '#default_value' => $this->getSetting('allow_edit'), + ]; $element['match_operator'] = [ '#type' => 'select', '#title' => $this->t('Autocomplete matching'), @@ -465,7 +471,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF // Make sure entity_access is not checked for unsaved entities. $entity_id = $entity->id(); - if (empty($entity_id) || $entity->access('update')) { + if (empty($entity_id) || ($settings['allow_edit'] && $entity->access('update'))) { $row['actions']['ief_entity_edit'] = [ '#type' => 'submit', '#value' => $this->t('Edit'), diff --git a/tests/src/FunctionalJavascript/ComplexWidgetTest.php b/tests/src/FunctionalJavascript/ComplexWidgetTest.php index 0c1e173d00bcaaa486d76203f8251f44f5a10cd9..df6e4bf526ea30617d4c3bba5d61d46b22fc4ac9 100644 --- a/tests/src/FunctionalJavascript/ComplexWidgetTest.php +++ b/tests/src/FunctionalJavascript/ComplexWidgetTest.php @@ -105,8 +105,9 @@ class ComplexWidgetTest extends InlineEntityFormTestBase { $assert_session->elementExists('xpath', $last_name_field_xpath); $assert_session->buttonExists('Create node'); - // Allow addition of existing nodes. + // Allow addition of existing nodes and disable edition. $this->updateSetting('allow_existing', TRUE); + $this->updateSetting('allow_edit', FALSE); // Asserts 'Add new node' form elements. $this->drupalGet($this->formContentAddUrl); @@ -355,7 +356,12 @@ class ComplexWidgetTest extends InlineEntityFormTestBase { $parent_node = $this->drupalGetNodeByTitle('Some title'); - // Edit the second entity. + // Unable to edit second entity, feature disabled. + $this->drupalGet('node/' . $parent_node->id() . '/edit'); + $assert_session->elementNotExists('xpath', '(//input[@value="Edit"])[2]'); + + // Edit the second entity once edit is enabled. + $this->updateSetting('allow_edit', TRUE); $this->drupalGet('node/' . $parent_node->id() . '/edit'); $assert_session->elementExists('xpath', '(//input[@value="Edit"])[2]')->press(); $this->assertNotEmpty($assert_session->waitForElement('xpath', $inner_title_field_xpath));