From b04d2ac1316c534e54d79453f0025caf10544a06 Mon Sep 17 00:00:00 2001
From: git <git@3442285.no-reply.drupal.org>
Date: Fri, 11 May 2018 11:35:03 +0200
Subject: [PATCH] Issue #2868155 by lyalyuk, gordon, acrosman, seanB,
 jOpdebeeck, yasmeensalah, and_daz, Alan D., sgurlt, Berdir, miro_dietiker,
 Jacine: Add paragraph bundle to widget forms to allow easier editing of
 paragraph forms

---
 .../FieldWidget/InlineParagraphsWidget.php    |  1 +
 .../Field/FieldWidget/ParagraphsWidget.php    |  1 +
 .../ParagraphsExperimentalAlterByTypeTest.php | 41 +++++++++++++++++++
 .../paragraphs_test/paragraphs_test.module    |  9 ++++
 4 files changed, 52 insertions(+)
 create mode 100644 src/Tests/Experimental/ParagraphsExperimentalAlterByTypeTest.php

diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
index 681182fd..22166257 100644
--- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
+++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
@@ -359,6 +359,7 @@ class InlineParagraphsWidget extends WidgetBase {
       $element += array(
         '#type' => 'container',
         '#element_validate' => array(array($this, 'elementValidate')),
+        '#paragraph_type' => $paragraphs_entity->bundle(),
         'subform' => array(
           '#type' => 'container',
           '#parents' => $element_parents,
diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
index 45f33cc9..34d82e4f 100644
--- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
+++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
@@ -445,6 +445,7 @@ class ParagraphsWidget extends WidgetBase {
       $element += array(
         '#type' => 'container',
         '#element_validate' => array(array($this, 'elementValidate')),
+        '#paragraph_type' => $paragraphs_entity->bundle(),
         'subform' => array(
           '#type' => 'container',
           '#parents' => $element_parents,
diff --git a/src/Tests/Experimental/ParagraphsExperimentalAlterByTypeTest.php b/src/Tests/Experimental/ParagraphsExperimentalAlterByTypeTest.php
new file mode 100644
index 00000000..b39a865d
--- /dev/null
+++ b/src/Tests/Experimental/ParagraphsExperimentalAlterByTypeTest.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Drupal\paragraphs\Tests\Experimental;
+
+/**
+ * Tests paragraphs experimental alter widget by type.
+ *
+ * @group paragraphs
+ */
+class ParagraphsExperimentalAlterByTypeTest extends ParagraphsExperimentalTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = [
+    'paragraphs_test',
+  ];
+
+
+  /**
+   * Test widget alter based on paragraph type
+   */
+  public function testAlterBasedOnType() {
+    $this->addParagraphedContentType('paragraphed_test', 'field_paragraphs', 'entity_reference_paragraphs');
+
+    $this->loginAsAdmin(['create paragraphed_test content', 'edit any paragraphed_test content']);
+    // Add a Paragraph type.
+    $paragraph_type = 'altered_paragraph';
+    $this->addParagraphsType($paragraph_type);
+
+    // Add a text field to the altered_paragraph type.
+    static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Normal title', 'text_long', [], []);
+
+    // Check that the form alteration based on Paragraphs type works.
+    // See paragraphs_test_field_widget_entity_reference_paragraphs_form_alter()
+    $this->drupalGet('node/add/paragraphed_test');
+    $this->assertText('Altered title');
+  }
+}
diff --git a/tests/modules/paragraphs_test/paragraphs_test.module b/tests/modules/paragraphs_test/paragraphs_test.module
index 5cc0e0ad..c5c345eb 100644
--- a/tests/modules/paragraphs_test/paragraphs_test.module
+++ b/tests/modules/paragraphs_test/paragraphs_test.module
@@ -41,3 +41,12 @@ function paragraphs_test_paragraph_view(array &$build, ParagraphInterface $entit
   $parent_field_name = $entity->get('parent_field_name')->value;
   \Drupal::messenger()->addStatus("Parent: $parent_type/$parent_id/$parent_field_name", TRUE);
 }
+
+/**
+ * Implements hook_field_widget_WIDGET_TYPE_form_alter().
+ */
+function paragraphs_test_field_widget_entity_reference_paragraphs_form_alter(&$element, &$form_state, $context) {
+  if ($element['#paragraph_type'] == 'altered_paragraph') {
+    $element['subform']['field_text']['widget'][0]['#title'] = 'Altered title';
+  }
+}
\ No newline at end of file
-- 
GitLab