From 02d7f6db36b172e800bc59e86487203d90a5f1b2 Mon Sep 17 00:00:00 2001
From: Oleh Shevchuk <oleh.shevchuck@evolvingweb.ca>
Date: Thu, 27 Jan 2022 14:37:54 +0000
Subject: [PATCH] Added the option to not open the default creation form for
 the required field

---
 config/schema/inline_entity_form.schema.yml        |  3 +++
 .../Field/FieldWidget/InlineEntityFormComplex.php  | 14 +++++++++++++-
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/config/schema/inline_entity_form.schema.yml b/config/schema/inline_entity_form.schema.yml
index b962119e..78ddce0e 100644
--- a/config/schema/inline_entity_form.schema.yml
+++ b/config/schema/inline_entity_form.schema.yml
@@ -51,6 +51,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 12cff2c1..74ca1d7f 100644
--- a/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
+++ b/src/Plugin/Field/FieldWidget/InlineEntityFormComplex.php
@@ -105,6 +105,7 @@ class InlineEntityFormComplex extends InlineEntityFormBase implements ContainerF
       'allow_existing' => FALSE,
       'match_operator' => 'CONTAINS',
       'allow_duplicate' => FALSE,
+      'auto_open' => TRUE,
     ];
 
     return $defaults;
@@ -118,6 +119,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']]),
@@ -181,6 +189,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');
+    }
+
     return $summary;
   }
 
@@ -439,7 +451,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;
-- 
GitLab