diff --git a/config/schema/inline_entity_form.schema.yml b/config/schema/inline_entity_form.schema.yml index 43b5d6525f11a5a7c03c07f34e8f238aa0853642..8a6d31105112859fc803d1b8d0e3b29a57c4571d 100644 --- a/config/schema/inline_entity_form.schema.yml +++ b/config/schema/inline_entity_form.schema.yml @@ -40,6 +40,9 @@ field.widget.settings.inline_entity_form_simple: revision: type: boolean label: "Create new revision" + auto_open_edit_form: + type: boolean + label: "Auto-open edit form" removed_reference: type: string label: "Keep or delete unreferenced items" diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index 5a9fb54de091778f8914e693e8d123863c509dea..71c6b49e54415133d7172086cce092a8f3c8bd7b 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -124,6 +124,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF 'match_operator' => 'CONTAINS', 'allow_duplicate' => FALSE, 'auto_open' => TRUE, + 'auto_open_edit_form' => FALSE, ]; return $defaults; @@ -176,6 +177,11 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF '#title' => $this->t('Allow users to duplicate @label.', ['@label' => $labels['plural']]), '#default_value' => $this->getSetting('allow_duplicate'), ]; + $element['auto_open_edit_form'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Auto-open the edit form when an entity is attached.'), + '#default_value' => $this->getSetting('auto_open_edit_form'), + ]; $description = $this->t('Select whether a @child should be deleted altogether if removed as a reference here.<br /> <em>Delete always</em> is recommended whenever each @child is exclusively managed within a single @parent without creating new revisions.<br /> @@ -230,6 +236,10 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $summary[] = $this->t('Automatically open creation form'); } + if ($this->getSetting('auto_open_edit_form')) { + $summary[] = $this->t('Auto-open edit form'); + } + switch ($this->getSetting('removed_reference')) { case self::REMOVED_KEEP: $summary[] = $this->t('Always keep unreferenced @label.', ['@label' => $labels['plural']]); @@ -247,6 +257,35 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF return $summary; } + /** + * {@inheritdoc} + */ + protected function prepareFormState(FormStateInterface $form_state, FieldItemListInterface $items, $translating = FALSE) { + $widget_state = $form_state->get(['inline_entity_form', $this->iefId]); + if (empty($widget_state)) { + $widget_state = [ + 'instance' => $this->fieldDefinition, + 'form' => NULL, + 'delete' => [], + 'entities' => [], + ]; + // Store the $items entities in the widget state, for further manipulation. + foreach ($items->referencedEntities() as $delta => $entity) { + // Display the entity in the correct translation. + if ($translating) { + $entity = TranslationHelper::prepareEntity($entity, $form_state); + } + $widget_state['entities'][$delta] = [ + 'entity' => $entity, + 'weight' => $delta, + 'form' => $this->getSetting('auto_open_edit_form') ? 'edit' : NULL, + 'needs_save' => $entity->isNew(), + ]; + } + $form_state->set(['inline_entity_form', $this->iefId], $widget_state); + } + } + /** * Returns the options for the match operator. *