diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index d44bd63..9704ecb 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,24 +441,33 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN /** * {@inheritdoc} */ - public function getSummary() { + public function getSummary($allowed_bundles = NULL, $count_limit = 5, $show_behavior_summary = TRUE) { $summary = []; + $this->summaryCount = 0; + + if ($this->bundle() !== NULL && (is_array($allowed_bundles) && !in_array($this->bundle(), $allowed_bundles))) { + return ''; + } + 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); + $nested_summary = $this->getNestedSummary($field_name, $allowed_bundles, $count_limit, $show_behavior_summary); if ($nested_summary != '') { + $this->summaryCount++; $summary[] = $nested_summary; } } @@ -459,6 +475,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(); } } @@ -466,10 +483,12 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN } - $paragraphs_type = $this->getParagraphType(); - foreach ($paragraphs_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin) { - if ($plugin_summary = $plugin->settingsSummary($this)) { - $summary = array_merge($summary, $plugin_summary); + if ($show_behavior_summary) { + $paragraphs_type = $this->getParagraphType(); + foreach ($paragraphs_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin) { + if ($plugin_summary = $plugin->settingsSummary($this)) { + $summary = array_merge($summary, $plugin_summary); + } } } @@ -478,6 +497,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 @@ -521,20 +555,54 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN * * @param string $field_name * Field definition id for paragraph. + * @param array|null $allowed_bundles + * Array of paragraph bundles to display. NULL means all are allowed and an + * empty array means no bundle is allowed. + * @param int $count_limit + * Number of how many summaries should be displayed. + * @param bool $show_behavior_summary + * Indicates if the behavior summary should be displayed. * * @return string * 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(); + protected function getNestedSummary($field_name, $allowed_bundles, $count_limit, $show_behavior_summary) { + $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($allowed_bundles, $count_limit, $show_behavior_summary); + } + } + + // If we start with the closed summary the $value['entity'] is not + // calculated so we load the entity ourselves. + elseif (isset($value['target_id']) && isset($value['target_revision_id'])) { + $paragraph_entity = $this->entityTypeManager() + ->getStorage('paragraph') + ->loadRevision($value['target_revision_id']); + if ($paragraph_entity) { + $summary[] = $paragraph_entity->getSummary(); + $summary_count += $paragraph_entity->getSummaryCount(); + } } } - return trim($summary); + if ($summary_count > $count_limit) { + return t('More then @count_limit children @summary', [ + '@count_limit' => $count_limit, + '@summary' => implode(', ', array_slice($summary, 0, $count_limit)), + ]); + } + + 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 5cdbcc8..e809d02 100644 --- a/src/ParagraphInterface.php +++ b/src/ParagraphInterface.php @@ -25,9 +25,17 @@ interface ParagraphInterface extends ContentEntityInterface, EntityOwnerInterfac /** * Returns short summary for paragraph. * + * @param array|null $allowed_bundles + * Array of paragraph bundles to display. NULL means all are allowed and an + * empty array means no bundle is allowed. + * @param int $count_limit + * Number of how many summaries should be displayed. + * @param bool $show_behavior_summary + * Indicates if the behavior summary should be displayed. + * * @return string * The text without tags. */ - public function getSummary(); + public function getSummary($allowed_bundles = NULL, $count_limit = 5, $show_behavior_summary = TRUE); } diff --git a/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php b/src/Tests/Experimental/ParagraphsExperimentalBehaviorsTest.php index 639202a..d59b8d2 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">1 child nested_paragraph, Text color: blue, Bold: No, Bold: Yes'); } /**