diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 3133f43..1dceb54 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -17,7 +17,6 @@ use Drupal\Core\Render\Element; use Drupal\paragraphs; use Symfony\Component\Validator\ConstraintViolationInterface; - /** * Plugin implementation of the 'entity_reference paragraphs' widget. * @@ -571,6 +570,21 @@ class InlineParagraphsWidget extends WidgetBase { $element['preview']['#access'] = $paragraphs_entity->access('view'); } elseif ($item_mode == 'closed') { + $violations = $paragraphs_entity->validate(); + $violations->filterByFieldAccess(); + foreach ($violations->getEntityViolations() as $violation) { + if (!empty($violation)) { + $render_array = [ + [ + '#theme' => 'item_list', + '#items' => $violation->getMessage(), + ], + ]; + $message = \Drupal::service('renderer')->renderPlain($render_array); + drupal_set_message($message, 'error'); + } + } + $element['subform'] = array(); } else { @@ -1046,6 +1060,9 @@ class InlineParagraphsWidget extends WidgetBase { $display->extractFormValues($entity, $element['subform'], $form_state); $display->validateFormValues($entity, $element['subform'], $form_state); } +// if ($widget_state['paragraphs'][$delta]['mode'] == 'closed'){ +// $display->extractFormValues($entity->validate(), $element['subform'], $form_state); +// } } static::setWidgetState($element['#field_parents'], $field_name, $form_state, $widget_state); diff --git a/src/Tests/ParagraphsAccessTest.php b/src/Tests/ParagraphsAccessTest.php index 3a2a6d2..1be223a 100644 --- a/src/Tests/ParagraphsAccessTest.php +++ b/src/Tests/ParagraphsAccessTest.php @@ -7,6 +7,7 @@ use Drupal\field_ui\Tests\FieldUiTestTrait; use Drupal\simpletest\WebTestBase; use Drupal\user\RoleInterface; use Drupal\user\Entity\Role; +use Drupal\paragraphs\Entity\Paragraph; /** * Tests the access check of paragraphs. @@ -159,4 +160,109 @@ class ParagraphsAccessTest extends WebTestBase { $node = $this->getNodeByTitle('delete_permissions'); $this->assertUrl('node/' . $node->id()); } + + /** + * Test validation of closed paragraphs. + */ + public function testValidationClosedParagraphs() { + // Create a Paragraphs with an entity reference field. + + $admin_user = $this->drupalCreateUser(array( + 'administer node fields', + 'administer paragraph form display', + 'administer node form display', + 'create paragraphed_content_demo content', + 'edit any paragraphed_content_demo content', + )); + $this->drupalLogin($admin_user); + // Adding Entity Reference to Paragraph Content Type. + $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', [ + 'new_storage_type' => 'field_ui:entity_reference:node', + 'label' => 'Content reference test', + 'field_name' => 'content', + ], t('Save and continue')); + + // Create a node. + $title = 'empty_node'; + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->drupalPostForm(NULL, ['title[0][value]' => $title], t('Save')); + $this->assertText('Paragraphed article ' . $title . ' has been created'); + + // Create a node and add it as an entity reference. + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->assertText('Content reference test'); + $paragraph = Paragraph::create([ + 'title' => 'Paragraph2', + 'type' => 'text', + 'langcode' => 'en', + 'field_text_demo' => 'english_text_1', + 'field_test_reference[0][target_id]' => $title, + ]); + + // Change the form-display to closed and delete referenced node. + $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/form-display'); + $edit = [ + 'fields[field_paragraphs_demo][settings_edit_form][settings][edit_mode]' => 'closed' + ]; + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_settings_edit'); + $this->drupalPostForm(NULL, $edit, t('Update')); + $this->drupalPostForm(NULL, [], 'Save'); + $this->drupalGetNodeByTitle($title)->delete(); + + // Check if the setting is stored. + $this->drupalGet('node/1/edit'); + + } + + /** + * Tests required field in collapsed paragraph. + */ + public function testRequiredField() { + $admin_user = $this->drupalCreateUser(array( + 'administer node fields', + 'administer paragraph form display', + 'administer node form display', + 'create paragraphed_content_demo content', + 'edit any paragraphed_content_demo content', + )); + $this->drupalLogin($admin_user); + + // Create a paragraph without required field. + $title = 'Empty'; + $this->drupalGet('node/add/paragraphed_content_demo'); + $this->drupalPostForm(NULL, ['title[0][value]' => $title], t('Save')); + $this->assertText('Paragraphed article ' . $title . ' has been created'); + + // Create content type that is required. + $this->drupalPostForm('admin/structure/types/manage/paragraphed_content_demo/fields/add-field', [ + 'new_storage_type' => 'field_ui:entity_reference:node', + 'label' => 'Required Content Test', + 'field_name' => 'content', + ], t('Save and continue')); + $this->drupalPostForm(NULL, [], t('Save field settings')); + $edit = [ + 'required' => TRUE, + 'settings[handler_settings][target_bundles][paragraphed_content_demo]' => TRUE, + ]; + $this->drupalPostForm(NULL, $edit, 'Save settings'); + $this->assertText('Saved Required Content Test configuration.'); + + // Access previously paragraph after it is created required content. + $this->drupalGet('node/1/edit'); + $this->drupalPostForm(NULL, [], t('Save')); + $this->assertText('Required Content Test field is required'); + + // Change form display to closed. + $this->drupalGet('admin/structure/types/manage/paragraphed_content_demo/form-display'); + $edit = [ + 'fields[field_paragraphs_demo][settings_edit_form][settings][edit_mode]' => 'closed' + ]; + $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_demo_settings_edit'); + $this->drupalPostForm(NULL, $edit, t('Update')); + $this->drupalPostForm(NULL, [], 'Save'); + + // Saving paragraph must trigger validation. + $this->drupalGet('node/1/edit'); + $this->drupalPostForm(NULL, [], t('Save')); + } }