diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index 0b430bb..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() { @@ -434,28 +441,28 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN /** * {@inheritdoc} */ - public function getSummary($nested = FALSE) { + public function getSummary() { $summary = []; - $count = 0; + $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 != '') { - $count += 1; + $this->summaryCount++; $summary[] = $file_summary; } } $text_summary = $this->getTextSummary($field_name, $field_definition); if ($text_summary != '') { - $count += 1; + $this->summaryCount++; $summary[] = $text_summary; } if ($field_definition->getType() == 'entity_reference_revisions') { $nested_summary = $this->getNestedSummary($field_name); if ($nested_summary != '') { - $count += 1; + $this->summaryCount++; $summary[] = $nested_summary; } } @@ -463,7 +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) { - $count += 1; + $this->summaryCount++; $summary[] = $this->get($field_name)->entity->label(); } } @@ -479,14 +486,22 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN } $collapsed_summary_text = implode(', ', $summary); + return strip_tags($collapsed_summary_text); + } - if ($nested == TRUE) { - return [ - 'count' => $count, - 'summary' => strip_tags($collapsed_summary_text), - ]; + /** + * Gets the number of summaries. + * + * @return int + * The number of summaries. Can be 0. + */ + protected function getSummaryCount() { + if ($this->summaryCount !== NULL) { + return $this->summaryCount; } - return strip_tags($collapsed_summary_text); + + $this->getSummary(); + return $this->summaryCount; } /** @@ -538,30 +553,23 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN * Short summary for nested paragraphs type. */ protected function getNestedSummary($field_name) { - $count = 0; $summary = []; + $summary_count = 0; foreach ($this->{$field_name}->getValue() as $value) { if (isset($value['entity'])) { $paragraph_entity = $value['entity']; if ($paragraph_entity instanceof ParagraphInterface) { - $info = $paragraph_entity->getSummary(TRUE); - $count += $info['count']; - $summary[] = trim($info['summary']); + $summary[] = $paragraph_entity->getSummary(); + $summary_count += $paragraph_entity->getSummaryCount(); } } } - if ($count == 1) { - return t('1 Child: @summary', [ - '@summary' => $summary[0], - ]); - } - else { - return t('@count Children: @summary', [ - '@count' => $count, + return \Drupal::translation() + ->formatPlural($summary_count, '1 Child | @summary', '@count Children | @summary', [ + '@count' => $summary_count, '@summary' => implode(', ', $summary), ]); - } } /** diff --git a/src/ParagraphInterface.php b/src/ParagraphInterface.php index aa2c1f9..5cdbcc8 100644 --- a/src/ParagraphInterface.php +++ b/src/ParagraphInterface.php @@ -25,12 +25,9 @@ interface ParagraphInterface extends ContentEntityInterface, EntityOwnerInterfac /** * Returns short summary for paragraph. * - * @param $nested bool - * Indicates if the summary is for a nested paragraph. - * - * @return string|array - * The text without tags or array of values for the nested paragraph. + * @return string + * The text without tags. */ - public function getSummary($nested); + public function getSummary(); } diff --git a/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php index e1cbdb9..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">0 Children: , Bold: Yes'); + $this->assertRaw('class="paragraphs-collapsed-description">0 Children | , Bold: Yes'); } /**