From 0deade822915c247570b9f746e365fdc4d88c528 Mon Sep 17 00:00:00 2001
From: justin2pin <justin2pin@278450.no-reply.drupal.org>
Date: Thu, 14 Jul 2022 15:18:32 +0000
Subject: [PATCH] Fixes issue with form parents and adds tests

---
 .../Behavior/LayoutParagraphsBehavior.php     |  4 +
 .../ParagraphsWidgetCompatibilityTest.php     | 99 +++++++++++++++++++
 2 files changed, 103 insertions(+)
 create mode 100644 tests/src/FunctionalJavascript/ParagraphsWidgetCompatibilityTest.php

diff --git a/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php b/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php
index 16c5baf..b9a9f08 100644
--- a/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php
+++ b/src/Plugin/paragraphs/Behavior/LayoutParagraphsBehavior.php
@@ -174,6 +174,10 @@ class LayoutParagraphsBehavior extends ParagraphsBehaviorBase {
     $filtered_values = $this->filterBehaviorFormSubmitValues($paragraph, $form, $form_state);
     $plugin_instance = $this->layoutPluginManager->createInstance($form_state->getValue('layout'), $form_state->getValue('config') ?? []);
     if ($plugin_form = $this->getLayoutPluginForm($plugin_instance)) {
+      // Add default #parents array to prevent form errors.
+      // @see https://www.drupal.org/project/layout_paragraphs/issues/3291180
+      $form['config'] += ['#parents' => []];
+      $form += ['#parents' => []];
       $subform_state = SubformState::createForSubform($form['config'], $form, $form_state);
       $plugin_form->submitConfigurationForm($form['config'], $subform_state);
       $filtered_values['config'] = $plugin_form->getConfiguration();
diff --git a/tests/src/FunctionalJavascript/ParagraphsWidgetCompatibilityTest.php b/tests/src/FunctionalJavascript/ParagraphsWidgetCompatibilityTest.php
new file mode 100644
index 0000000..19f6534
--- /dev/null
+++ b/tests/src/FunctionalJavascript/ParagraphsWidgetCompatibilityTest.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Drupal\Tests\layout_paragraphs\FunctionalJavascript;
+
+use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
+use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
+
+/**
+ * Tests basic compatibility with default paragraphs form widget.
+ *
+ * @group layout_paragraphs
+ */
+class ParagraphsWidgetCompatibilityTest extends WebDriverTestBase {
+
+  use ParagraphsTestBaseTrait;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected static $modules = [
+    'layout_paragraphs',
+    'paragraphs',
+    'node',
+    'field',
+    'field_ui',
+    'block',
+    'paragraphs_test',
+  ];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected $defaultTheme = 'claro';
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp(): void {
+    parent::setUp();
+
+    $this->addParagraphsType('section');
+    $this->addFieldtoParagraphType('section', 'field_text', 'text');
+
+    $user = $this->drupalCreateUser([
+      'administer site configuration',
+      'administer node fields',
+      'administer node display',
+      'administer node form display',
+      'administer paragraphs types',
+    ]);
+    $this->drupalLogin($user);
+
+    // Enable Layout Paragraphs behavior for section paragraph type.
+    $this->drupalGet('admin/structure/paragraphs_type/section');
+    $this->submitForm([
+      'behavior_plugins[layout_paragraphs][enabled]' => TRUE,
+      'behavior_plugins[layout_paragraphs][settings][available_layouts][]' => [
+        'layout_onecol',
+        'layout_twocol',
+        'layout_threecol_25_50_25',
+        'layout_threecol_33_34_33',
+      ],
+    ], 'Save');
+    $this->assertSession()->pageTextContains('Saved the section Paragraphs type.');
+    $this->drupalGet('admin/structure/paragraphs_type/section');
+    $this->addParagraphedContentType('page', 'field_content');
+    $this->drupalGet('admin/structure/types/manage/page/fields/node.page.field_content');
+    $this->submitForm([
+      'settings[handler_settings][negate]' => '1',
+    ], 'Save settings');
+    $this->drupalGet('admin/structure/types/manage/page/form-display');
+    $page = $this->getSession()->getPage();
+    $btn = $page->find('css', '#edit-fields-field-content-settings-edit');
+    $btn->click();
+    $this->assertSession()->assertWaitOnAjaxRequest();
+    $page->selectFieldOption('fields[field_content][settings_edit_form][settings][default_paragraph_type]', 'section');
+    $this->submitForm([], 'Save');
+    $this->drupalLogout();
+  }
+
+  /**
+   * Tests that adding a new page works without errors.
+   */
+  public function testAddPage() {
+    $user = $this->drupalCreateUser([
+      'create page content',
+      'edit own page content',
+      'edit behavior plugin settings',
+    ]);
+    $this->drupalLogin($user);
+    $this->drupalGet('node/add/page');
+    $this->submitForm([
+      'title[0][value]' => 'Node title',
+    ], 'Save');
+    $this->assertSession()->pageTextNotContains('The website encountered an unexpected error. Please try again later.');
+
+  }
+
+}
-- 
GitLab