diff --git a/config/schema/inline_entity_form.schema.yml b/config/schema/inline_entity_form.schema.yml index 3206d31c957e0ad3f2becd2a247c023dd13e337a..a54733d4e3f99ff7fccc843621ffb1f9a23a3f38 100644 --- a/config/schema/inline_entity_form.schema.yml +++ b/config/schema/inline_entity_form.schema.yml @@ -60,6 +60,9 @@ field.widget.settings.inline_entity_form_complex: label_plural: type: label label: "Label (plural)" + auto_open: + type: boolean + label: "Automatically open entity creation form" allow_new: type: boolean label: "Allow new" diff --git a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php index a2e21e78c9d1ba204043c7735d1219e7b8da1c67..229cef737fdbd916fc78178b26733e5ecb15fba2 100644 --- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php +++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php @@ -122,6 +122,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF 'removed_reference' => self::REMOVED_OPTIONAL, 'match_operator' => 'CONTAINS', 'allow_duplicate' => FALSE, + 'auto_open' => TRUE, ]; return $defaults; @@ -135,6 +136,13 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $labels = $this->getEntityTypeLabels(); $states_prefix = 'fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings]'; + if ($this->fieldDefinition->isRequired()) { + $element['auto_open'] = [ + '#type' => 'checkbox', + '#title' => $this->t('Automatically open entity creation form'), + '#default_value' => $this->getSetting('auto_open'), + ]; + } $element['allow_new'] = [ '#type' => 'checkbox', '#title' => $this->t('Allow users to add new @label.', ['@label' => $labels['plural']]), @@ -212,6 +220,10 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $summary[] = $this->t('@label can not be duplicated.', ['@label' => $labels['plural']]); } + if ($this->getSetting('auto_open') && $this->fieldDefinition->isRequired()) { + $summary[] = $this->t('Automatically open creation form'); + } + switch ($this->getSetting('removed_reference')) { case self::REMOVED_KEEP: $summary[] = $this->t('Always keep unreferenced @label.', ['@label' => $labels['plural']]); @@ -558,7 +570,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF $allow_new = $settings['allow_new'] && !empty($create_bundles); $hide_cancel = FALSE; // If the field is required and empty try to open one of the forms. - if (empty($entities) && $this->fieldDefinition->isRequired()) { + if (empty($entities) && $this->getSetting('auto_open') && $this->fieldDefinition->isRequired()) { if ($settings['allow_existing'] && !$allow_new) { $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], 'ief_add_existing'); $hide_cancel = TRUE;