diff --git a/tests/modules/paragraphs_test/paragraphs_test.module b/tests/modules/paragraphs_test/paragraphs_test.module index 7960532..9d57dfd 100644 --- a/tests/modules/paragraphs_test/paragraphs_test.module +++ b/tests/modules/paragraphs_test/paragraphs_test.module @@ -5,14 +5,14 @@ * Test module for testing the paragraphs module. */ -use Drupal\Core\Entity\Display\EntityViewDisplayInterface; +use Drupal\Core\Render\Element; use Drupal\paragraphs\ParagraphInterface; use Drupal\paragraphs\Plugin\Field\FieldWidget\ParagraphsWidget; /** * Implements hook_paragraphs_widget_actions_alter(). */ -function paragraphs_test_paragraphs_widget_actions_alter(&$widget_actions, &$context) { +function paragraphs_test_paragraphs_widget_actions_alter(&$widget_actions) { if (\Drupal::state()->get('paragraphs_test_dropbutton')) { $widget_actions['dropdown_actions']['test_button'] = ParagraphsWidget::expandButton([ '#type' => 'submit', @@ -29,7 +29,7 @@ function paragraphs_test_paragraphs_widget_actions_alter(&$widget_actions, &$con /** * Implements hook_ENTITY_TYPE_view(). */ -function paragraphs_test_paragraph_view(array &$build, ParagraphInterface $entity, EntityViewDisplayInterface $display, $view_mode) { +function paragraphs_test_paragraph_view(array &$build, ParagraphInterface $entity) { if (!\Drupal::state('paragraphs_test_parent')) { return; } @@ -38,3 +38,55 @@ function paragraphs_test_paragraph_view(array &$build, ParagraphInterface $entit $parent_field_name = $entity->get('parent_field_name')->value; drupal_set_message("Parent: $parent_type/$parent_id/$parent_field_name", 'status', TRUE); } + +/** + * Implements hook_form_paragraphs_subform_alter(). + */ +function paragraphs_test_form_paragraphs_subform_alter(array &$subform) { + foreach (Element::children($subform) as $key) { + // Check if container suffix empty. Needs to avoid join warning. + if (empty($subform[$key]['#suffix'])) { + $subform[$key]['#suffix'] = ''; + } + $subform[$key]['#suffix'] .= 'Here is element added by hook_form_paragraphs_subform_alter(). Key - ' . $key . '
'; + } +} + +/** + * Implements hook_form_paragraphs_subform_TYPE_alter(). + */ +function paragraphs_test_form_paragraphs_subform_text_paragraph_alter(array &$subform) { + foreach (Element::children($subform) as $key) { + // Check if container suffix empty. Needs to avoid join warning. + if (empty($subform[$key]['#suffix'])) { + $subform[$key]['#suffix'] = ''; + } + $subform[$key]['#suffix'] .= 'Here is subform for paragraph type `text_paragraph` added by hook_form_paragraphs_subform_TYPE_alter()
'; + } +} + +/** + * Implements hook_form_paragraphs_subform_WIDGET_alter(). + */ +function paragraphs_test_form_paragraphs_subform_experimental_alter(array &$subform) { + foreach (Element::children($subform) as $key) { + // Check if container suffix empty. Needs to avoid join warning. + if (empty($subform[$key]['#suffix'])) { + $subform[$key]['#suffix'] = ''; + } + $subform[$key]['#suffix'] .= 'Here is subform for experimental paragraph widget added by hook_form_paragraphs_subform_WIDGET_alter()
'; + } +} + +/** + * Implements hook_form_paragraphs_subform_WIDGET_TYPE_alter(). + */ +function paragraphs_test_form_paragraphs_subform_experimental_text_paragraph_alter(array &$subform) { + foreach (Element::children($subform) as $key) { + // Check if container suffix empty. Needs to avoid join warning. + if (empty($subform[$key]['#suffix'])) { + $subform[$key]['#suffix'] = ''; + } + $subform[$key]['#suffix'] .= 'Here is subform for experimental paragraph widget type `text_paragraph` added by hook_form_paragraphs_subform_WIDGET_TYPE_alter()
'; + } +} diff --git a/tests/src/Functional/ParagraphsExperimentalSubformTest.php b/tests/src/Functional/ParagraphsExperimentalSubformTest.php new file mode 100644 index 0000000..4e06a87 --- /dev/null +++ b/tests/src/Functional/ParagraphsExperimentalSubformTest.php @@ -0,0 +1,115 @@ +addParagraphedContentType('paragraphed_test'); + + $permissions = [ + 'administer content types', + 'administer node fields', + 'administer paragraphs types', + 'administer node form display', + 'administer paragraph fields', + 'administer paragraph form display', + 'create paragraphed_test content', + 'edit any paragraphed_test content', + ]; + $this->loginAsAdmin($permissions, TRUE); + + // Add new Paragraph types. + $this->addParagraphsType('text_paragraph'); + $this->addFieldtoParagraphType('text_paragraph', 'field_text', 'text_long'); + + $this->addParagraphsType('another_paragraph'); + $this->addFieldtoParagraphType('another_paragraph', 'field_title', 'string'); + + // Test for the node add form. + // New paragraphs. + $this->drupalGet('node/add/paragraphed_test'); + $this->getSession()->getPage()->findButton('field_paragraphs_text_paragraph_add_more')->press(); + $this->getSession()->getPage()->findButton('field_paragraphs_another_paragraph_add_more')->press(); + + // Checks that text added by hook_form_paragraphs_subform_alter() exists. + $this->assertSession()->pageTextContains('Here is element added by hook_form_paragraphs_subform_alter(). Key - field_text'); + $this->assertSession()->pageTextContains('Here is element added by hook_form_paragraphs_subform_alter(). Key - field_title'); + + // Checks text added by hook_form_paragraphs_subform_TYPE_alter() exists. + $this->assertSession()->pageTextContains('Here is subform for paragraph type `text_paragraph` added by hook_form_paragraphs_subform_TYPE_alter()'); + + // Checks text added by hook_form_paragraphs_subform_WIDGET_alter() exists. + $this->assertSession()->pageTextContains('Here is subform for experimental paragraph widget added by hook_form_paragraphs_subform_WIDGET_alter()'); + + // Checks text added by + // hook_form_paragraphs_subform_WIDGET_TYPE_alter() exists. + $this->assertSession()->pageTextContains('Here is subform for experimental paragraph widget type `text_paragraph` added by hook_form_paragraphs_subform_WIDGET_TYPE_alter()'); + + // Check for node editing with closed paragraphs. + $edit = [ + 'title[0][value]' => 'Paragraphs Subform test node', + 'field_paragraphs[0][subform][field_text][0][value]' => 'Text paragraph', + 'field_paragraphs[1][subform][field_title][0][value]' => 'Title paragraph', + ]; + $this->drupalPostForm(NULL, $edit, t('Save')); + $node = $this->drupalGetNodeByTitle('Paragraphs Subform test node'); + + // Set the settings to "Closed" to check that subforms are clean. + $settings = [ + 'edit_mode' => 'closed', + 'closed_mode' => 'summary', + 'autocollapse' => 'none', + ]; + $this->setParagraphsWidgetSettings('paragraphed_test', 'field_paragraphs', $settings); + + $this->drupalGet('node/' . $node->id() . '/edit'); + + // Check if all subforms are closed. + $this->assertSession()->pageTextNotContains('Here is element added by'); + $this->assertSession()->pageTextNotContains('Here is subform for experimental paragraph widget'); + + // Open first `text_paragraph`. + $this->getSession()->getPage()->findButton('field_paragraphs_0_edit')->press(); + + // It should contains all texts except 'Here is element added by + // hook_form_paragraphs_subform_alter(). Key - field_title'. + $this->assertSession()->pageTextContains('Here is element added by hook_form_paragraphs_subform_alter(). Key - field_text'); + $this->assertSession()->pageTextContains('Here is subform for paragraph type `text_paragraph` added by hook_form_paragraphs_subform_TYPE_alter()'); + $this->assertSession()->pageTextContains('Here is subform for experimental paragraph widget added by hook_form_paragraphs_subform_WIDGET_alter()'); + $this->assertSession()->pageTextContains('Here is subform for experimental paragraph widget type `text_paragraph` added by hook_form_paragraphs_subform_WIDGET_TYPE_alter()'); + $this->assertSession()->pageTextNotContains('Here is element added by hook_form_paragraphs_subform_alter(). Key - field_title'); + + } + +}