From ad117e41d3f7b26644732049cdbbb40709c9f4a6 Mon Sep 17 00:00:00 2001
From: Justin Toupin <21499-justin2pin@users.noreply.drupalcode.org>
Date: Wed, 14 Aug 2024 17:31:51 +0000
Subject: [PATCH] Issue #3468162: Allow default values for paragraphs to be
 passed to InsertComponentForm

---
 src/Form/InsertComponentForm.php | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/src/Form/InsertComponentForm.php b/src/Form/InsertComponentForm.php
index ce6f981..5a2372d 100644
--- a/src/Form/InsertComponentForm.php
+++ b/src/Form/InsertComponentForm.php
@@ -26,6 +26,13 @@ class InsertComponentForm extends ComponentFormBase {
    */
   protected $domSelector;
 
+  /**
+   * Default values for new paragraph.
+   *
+   * @var array
+   */
+  protected $paragraphDefaults = [];
+
   /**
    * The jQuery insertion method to use for adding the new component.
    *
@@ -84,6 +91,8 @@ class InsertComponentForm extends ComponentFormBase {
    *   The uuid of the sibling component.
    * @param string $placement
    *   Where to place the new component - either "before" or "after".
+   * @param array $paragraph_defaults
+   *   Default values for the new paragraph.
    */
   public function buildForm(
     array $form,
@@ -93,9 +102,13 @@ class InsertComponentForm extends ComponentFormBase {
     string $parent_uuid = NULL,
     string $region = NULL,
     string $sibling_uuid = NULL,
-    string $placement = NULL
+    string $placement = NULL,
+    array $paragraph_defaults = []
     ) {
 
+    if (!empty($paragraph_defaults)) {
+      $this->paragraphDefaults = $paragraph_defaults;
+    }
     $this->setLayoutParagraphsLayout($layout_paragraphs_layout);
     $langcode = $this->layoutParagraphsLayout->getEntity()->language()->getId();
     $this->paragraph = $this->newParagraph($paragraph_type, $langcode);
@@ -231,7 +244,7 @@ class InsertComponentForm extends ComponentFormBase {
         $bundle_key => $paragraph_type->id(),
         $langcode_key => $langcode,
         '_layoutParagraphsLayout' => $this->getLayoutParagraphsLayout(),
-      ]);
+      ] + $this->paragraphDefaults);
     return $paragraph;
   }
 
-- 
GitLab