diff -u b/src/Entity/Paragraph.php b/src/Entity/Paragraph.php --- b/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -451,24 +451,22 @@ if ($field_definition->getType() == 'image' || $field_definition->getType() == 'file') { $file_summary = $this->getFileSummary($field_name); if ($file_summary != '') { - $this->summaryCount++; $summary[] = $file_summary; } } $text_summary = $this->getTextSummary($field_name, $field_definition); if ($text_summary != '') { - $this->summaryCount++; $summary[] = $text_summary; } if ($field_definition->getType() == 'entity_reference_revisions') { + // Decrease the depth, since we are entering a nested paragraph. $nested_summary = $this->getNestedSummary($field_name, [ 'show_behavior_summary' => $show_behavior_summary, - 'depth_limit' => $depth_limit + 'depth_limit' => $depth_limit - 1 ]); if ($nested_summary != '') { - $this->summaryCount++; $summary[] = $nested_summary; } } @@ -476,7 +474,6 @@ if ($field_type = $field_definition->getType() == 'entity_reference') { if (!in_array($field_name, ['type', 'uid', 'revision_uid'])) { if ($this->get($field_name)->entity) { - $this->summaryCount++; $summary[] = $this->get($field_name)->entity->label(); } } @@ -493,6 +490,10 @@ } } + if ($this->summaryCount) { + array_unshift($summary, (string) \Drupal::translation()->formatPlural($this->summaryCount, '1 child', '@count children')); + } + $collapsed_summary_text = implode(', ', $summary); return strip_tags($collapsed_summary_text); } @@ -565,37 +566,24 @@ */ protected function getNestedSummary($field_name, array $options) { $summary = []; - $summary_count = 0; - // Decrease the depth, since we are entering a nested paragraph. - $options['depth_limit'] = $options['depth_limit'] - 1; if ($options['depth_limit'] >= 0) { - foreach ($this->{$field_name}->getValue() as $value) { - if (isset($value['target_id']) && isset($value['target_revision_id'])) { - $paragraph_entity = isset($value['entity']) ? $value['entity'] : $this->entityTypeManager() - ->getStorage('paragraph') - ->loadRevision($value['target_revision_id']); - if ($paragraph_entity instanceof ParagraphInterface) { - $summary[] = $paragraph_entity->getSummary($options); - $summary_count += $paragraph_entity->getSummaryCount(); - } + foreach ($this->get($field_name) as $item) { + $entity = $item->entity; + if ($entity instanceof ParagraphInterface) { + $summary[] = $entity->getSummary($options); + $this->summaryCount++; } } } - else { - $summary_count = count($this->{$field_name}); - } - $paragraph_summary = !empty($summary) ? ' | ' . implode(', ', $summary) : ''; - // Checks if it is an empty nested paragraph. - if ($summary_count === 0) { - return t('Empty paragraph@summary', ['@summary' => $paragraph_summary]); + $summary = array_filter($summary); + + if (empty($summary)) { + return NULL; } - return \Drupal::translation() - ->formatPlural($summary_count, '1 child@summary', '@count children @summary', [ - '@count' => $summary_count, - '@summary' => $paragraph_summary, - ]); + $paragraph_summary = implode(', ', $summary); + return $paragraph_summary; } /** diff -u b/src/ParagraphInterface.php b/src/ParagraphInterface.php --- b/src/ParagraphInterface.php +++ b/src/ParagraphInterface.php @@ -27,9 +27,10 @@ * @param array $options * (optional) Array of additional options, with the following elements: * - 'show_behavior_summary': Whether the summary should contain the - * behavior settings. TRUE enforces behavior settings in summary. + * behavior settings. Defaults to TRUE to show behavior settings in the + * summary. * - 'depth_limit': Depth limit of how many nested paragraph summaries are - * allowed. + * allowed. Defaults to 1 to show nested paragraphs only on top level. * * @return string * The text without tags. diff -u b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php --- b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php @@ -191,19 +191,7 @@ // Assert that the summary includes the text of the behavior plugins. $this->clickLink('Edit'); $this->assertRaw('class="paragraphs-collapsed-description">first_paragraph, Text color: blue, Bold: Yes'); - $this->assertRaw('class="paragraphs-collapsed-description">1 child | nested_paragraph, Text color: blue, Bold: No, Bold: Yes'); - - // Add an empty nested paragraph. - $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more'); - $edit = [ - 'title[0][value]' => 'collapsed_test', - ]; - $this->drupalPostForm(NULL, $edit, t('Save and publish')); - - // Check an empty nested paragraph summary. - $this->clickLink('Edit'); - $this->assertRaw('class="paragraphs-collapsed-description">Empty paragraph'); - + $this->assertRaw('class="paragraphs-collapsed-description">nested_paragraph, Text color: blue, Bold: No, Bold: Yes'); } /** @@ -251,7 +239,19 @@ // Assert that the summary includes the text of the behavior plugins. $this->clickLink('Edit'); $this->assertRaw('class="paragraphs-collapsed-description">first_paragraph, Text color: blue, Bold: Yes'); - $this->assertRaw('class="paragraphs-collapsed-description">nested_paragraph, Text color: blue, Bold: No, Bold: Yes'); + $this->assertRaw('class="paragraphs-collapsed-description">1 child | nested_paragraph, Text color: blue, Bold: No, Bold: Yes'); + + // Add an empty nested paragraph. + $this->drupalPostAjaxForm('node/add/paragraphed_test', [], 'field_paragraphs_nested_paragraph_add_more'); + $edit = [ + 'title[0][value]' => 'collapsed_test', + ]; + $this->drupalPostForm(NULL, $edit, t('Save and publish')); + + // Check an empty nested paragraph summary. + $this->clickLink('Edit'); + $this->assertRaw('class="paragraphs-collapsed-description">Empty paragraph'); + } /** diff -u b/tests/src/Kernel/ParagraphsCollapsedSummaryTest.php b/tests/src/Kernel/ParagraphsCollapsedSummaryTest.php --- b/tests/src/Kernel/ParagraphsCollapsedSummaryTest.php +++ b/tests/src/Kernel/ParagraphsCollapsedSummaryTest.php @@ -28,7 +28,6 @@ 'field', 'entity_reference_revisions', 'paragraphs_test', - 'paragraphs_demo' ]; /** @@ -40,12 +39,7 @@ $this->installEntitySchema('paragraph'); $this->installSchema('system', ['sequences']); \Drupal::moduleHandler()->loadInclude('paragraphs', 'install'); - } - /** - * Tests the collapsed summary additional options. - */ - public function testCollapsedSummaryOptions() { // Create a text paragraph type with test_text_color plugin enabled. $paragraph_type = ParagraphsType::create(array( 'label' => 'text_paragraph', @@ -66,7 +60,12 @@ ]); $paragraphs_type->save(); $this->addParagraphsField('nested_paragraph', 'nested_paragraph_field', 'entity_reference_revisions', ['target_type' => 'paragraph']); + } + /** + * Tests the collapsed summary additional options. + */ + public function testCollapsedSummaryOptions() { // Create a paragraph and set its feature settings. $paragraph = Paragraph::create([ 'type' => 'text_paragraph', @@ -91,9 +90,44 @@ 'nested_paragraph_field' => [$paragraph], ]); $paragraph_1->save(); - $this->assertEquals($paragraph_1->getSummary(), '1 child | Example text for a text paragraph, Text color: red'); - $this->assertEquals($paragraph_1->getSummary(['show_behavior_summary' => FALSE]), '1 child | Example text for a text paragraph'); - $this->assertEquals($paragraph_1->getSummary(['depth_limit' => 0]), '1 child'); + $this->assertEquals($paragraph_1->getSummary(), '1 child, Example text for a text paragraph, Text color: red'); + $this->assertEquals($paragraph_1->getSummary(['show_behavior_summary' => FALSE]), '1 child, Example text for a text paragraph'); + $this->assertEquals($paragraph_1->getSummary(['depth_limit' => 0]), ''); + } + + /** + * Tests nested paragraph summary. + */ + public function testNestedParagraphSummary() { + // Create a text paragraph. + $paragraph_text_1 = Paragraph::create([ + 'type' => 'text_paragraph', + 'text' => 'Text paragraph on nested level', + ]); + $paragraph_text_1->save(); + + // Add a nested paragraph with the text inside. + $paragraph_nested_1 = Paragraph::create([ + 'type' => 'nested_paragraph', + 'nested_paragraph_field' => [$paragraph_text_1], + ]); + $paragraph_nested_1->save(); + + // Create a new text paragraph. + $paragraph_text_2 = Paragraph::create([ + 'type' => 'text_paragraph', + 'text' => 'Text paragraph on top level', + ]); + $paragraph_text_2->save(); + + // Add a nested paragraph with the new text and nested paragraph inside. + $paragraph_nested_2 = Paragraph::create([ + 'type' => 'nested_paragraph', + 'nested_paragraph_field' => [$paragraph_text_2, $paragraph_nested_1], + ]); + $paragraph_nested_2->save(); + $this->assertEquals($paragraph_nested_2->getSummary(['show_behavior_summary' => FALSE]), '2 children, Text paragraph on top level'); + $this->assertEquals($paragraph_nested_2->getSummary(['show_behavior_summary' => FALSE, 'depth_limit' => 2]), '2 children, Text paragraph on top level, 1 child, Text paragraph on nested level'); } /**