diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index d44bd63..ed65403 100644 --- a/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -87,6 +87,13 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN protected $unserializedBehaviorSettings; /** + * Number of summaries. + * + * @var int + */ + protected $summaryCount; + + /** * {@inheritdoc} */ public function getParentEntity() { @@ -436,22 +443,26 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN */ public function getSummary() { $summary = []; + $this->summaryCount = 0; foreach ($this->getFieldDefinitions() as $field_name => $field_definition) { 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') { $nested_summary = $this->getNestedSummary($field_name); if ($nested_summary != '') { + $this->summaryCount++; $summary[] = $nested_summary; } } @@ -459,6 +470,7 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN 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(); } } @@ -478,6 +490,21 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN } /** + * Gets the number of summaries. + * + * @return int + * The number of summaries. Can be 0. + */ + protected function getSummaryCount() { + if ($this->summaryCount !== NULL) { + return $this->summaryCount; + } + + $this->getSummary(); + return $this->summaryCount; + } + + /** * Returns summary for file paragraph. * * @param string $field_name @@ -526,15 +553,23 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN * Short summary for nested paragraphs type. */ protected function getNestedSummary($field_name) { - $summary = ''; - if ($this->get($field_name)->entity) { - $paragraph_entity = $this->get($field_name)->entity; - if ($paragraph_entity instanceof ParagraphInterface) { - $summary = $paragraph_entity->getSummary(); + $summary = []; + $summary_count = 0; + foreach ($this->{$field_name}->getValue() as $value) { + if (isset($value['entity'])) { + $paragraph_entity = $value['entity']; + if ($paragraph_entity instanceof ParagraphInterface) { + $summary[] = $paragraph_entity->getSummary(); + $summary_count += $paragraph_entity->getSummaryCount(); + } } } - return trim($summary); + return \Drupal::translation() + ->formatPlural($summary_count, '1 Child | @summary', '@count Children | @summary', [ + '@count' => $summary_count, + '@summary' => implode(', ', $summary), + ]); } /** diff --git a/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php index 639202a..d4b4c61 100644 --- a/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php +++ b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php @@ -190,7 +190,7 @@ class ParagraphsExperimentalBehaviorsTest extends ParagraphsExperimentalTestBase // 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">0 Children | , Bold: Yes'); } /**