From d2f10cd7766acb619bb97d0ae4cef7b846d3ec39 Mon Sep 17 00:00:00 2001
From: Justin Toupin <justin@atendesigngroup.com>
Date: Wed, 22 Jul 2020 09:23:25 -0600
Subject: [PATCH] Adds compatibility with Inline Entity Form.

---
 .../layout_paragraphs_ief.info.yml            |  9 +++++
 .../layout_paragraphs_ief.module              | 40 +++++++++++++++++++
 .../FieldWidget/LayoutParagraphsWidget.php    | 23 ++++++++++-
 3 files changed, 71 insertions(+), 1 deletion(-)
 create mode 100644 modules/layout_paragraphs_ief/layout_paragraphs_ief.info.yml
 create mode 100644 modules/layout_paragraphs_ief/layout_paragraphs_ief.module

diff --git a/modules/layout_paragraphs_ief/layout_paragraphs_ief.info.yml b/modules/layout_paragraphs_ief/layout_paragraphs_ief.info.yml
new file mode 100644
index 00000000..aac18cb9
--- /dev/null
+++ b/modules/layout_paragraphs_ief/layout_paragraphs_ief.info.yml
@@ -0,0 +1,9 @@
+name: 'Layout Paragraphs IEF Support'
+type: module
+description: 'Provides support for using Inline Entity Forms within Layout Paragraphs.'
+core: 8.x
+core_version_requirement: ^8 || ^9
+package: 'Paragraphs'
+dependencies:
+  - drupal:layout_paragraphs
+  - drupal:inline_entity_form
diff --git a/modules/layout_paragraphs_ief/layout_paragraphs_ief.module b/modules/layout_paragraphs_ief/layout_paragraphs_ief.module
new file mode 100644
index 00000000..d06d5cc4
--- /dev/null
+++ b/modules/layout_paragraphs_ief/layout_paragraphs_ief.module
@@ -0,0 +1,40 @@
+<?php
+
+use Drupal\Component\Utility\NestedArray;
+use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Render\Element;
+use Drupal\inline_entity_form\ElementSubmit;
+use Drupal\inline_entity_form\WidgetSubmit;
+use Drupal\inline_entity_form\Form\EntityInlineForm;
+use Drupal\inline_entity_form\Plugin\Field\FieldWidget\InlineEntityFormComplex;
+
+/**
+ * Implements hook_form_alter().
+ */
+function layout_paragraphs_ief_form_alter(&$form, FormStateInterface $form_state, $form_id) {
+
+  // Attach the IEF handlers only if the current form has an IEF widget.
+  $widget_state = $form_state->get('inline_entity_form');
+  if (!is_null($widget_state)) {
+    $layout_paragraphs_widgets = $form_state->get('layout_paragraph_widgets');
+    if (!empty($layout_paragraphs_widgets)) {
+      foreach ($layout_paragraphs_widgets as $layout_paragraphs_widget) {
+        $entity_form_path = array_merge($layout_paragraphs_widget, ['entity_form']);
+        $entity_form = NestedArray::getValue($form, $entity_form_path);
+        if (!empty($entity_form)) {
+          ElementSubmit::attach($entity_form, $form_state);
+          WidgetSubmit::attach($entity_form, $form_state);
+          NestedArray::setValue($form, $entity_form_path, $entity_form);
+        }
+      }
+    }
+  }
+}
+
+function _layout_paragraphs_ief_traverse_form($form) {
+  foreach ($form as $item) {
+    $test = '';
+
+  }
+}
+
diff --git a/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php b/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
index 5038a03e..dca7d38a 100644
--- a/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
+++ b/src/Plugin/Field/FieldWidget/LayoutParagraphsWidget.php
@@ -771,6 +771,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
     ];
     // Add layout_paragraphs_widget library.
     $elements['#attached']['library'][] = 'layout_paragraphs/layout_paragraphs_widget';
+    $elements['#process'] = [[$this, 'registerLayoutParagraphsWidget']];
     return $elements;
   }
 
@@ -787,6 +788,26 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
     return $elements;
   }
 
+  /**
+   * Stores mapping to this widget in form_state.
+   *
+   * This allows other modules to more easily find, interact with or enhance
+   * layout paragraph widgets.
+   *
+   * @param array $elements
+   *   The widget form elements.
+   * @param Drupal\Core\Form\FormStateInterface $form_state
+   *   The form state.
+   */
+  public function registerLayoutParagraphsWidget(array $elements, FormStateInterface $form_state) {
+
+    $widgets = $form_state->get('layout_paragraph_widgets') ?? [];
+    $widgets[] = $elements['#array_parents'];
+    $form_state->set('layout_paragraph_widgets', $widgets);
+    return $elements;
+
+  }
+
   /**
    * Restructures $elements array into layout.
    *
@@ -1160,7 +1181,7 @@ class LayoutParagraphsWidget extends WidgetBase implements ContainerFactoryPlugi
         '#weight' => 1000,
         '#type' => 'actions',
         '#attributes' => ['class' => ['layout-paragraphs-item-form-actions']],
-        'save_item' => [
+        'submit' => [
           '#type' => 'submit',
           '#name' => 'save',
           '#value' => $this->t('Save'),
-- 
GitLab