diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index bcbaaf0..9704ecb 100644 --- a/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -441,9 +441,14 @@ 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); @@ -460,7 +465,7 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN } 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; @@ -478,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); + } } } @@ -548,11 +555,18 @@ 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) { + protected function getNestedSummary($field_name, $allowed_bundles, $count_limit, $show_behavior_summary) { $summary = []; $summary_count = 0; foreach ($this->{$field_name}->getValue() as $value) { @@ -560,22 +574,32 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN $paragraph_entity = $value['entity']; if ($paragraph_entity instanceof ParagraphInterface) { $summary[] = $paragraph_entity->getSummary(); - $summary_count += $paragraph_entity->getSummaryCount(); + $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 = \Drupal::entityTypeManager() + $paragraph_entity = $this->entityTypeManager() ->getStorage('paragraph') ->loadRevision($value['target_revision_id']); - $summary[] = $paragraph_entity->getSummary(); - $summary_count += $paragraph_entity->getSummaryCount(); + if ($paragraph_entity) { + $summary[] = $paragraph_entity->getSummary(); + $summary_count += $paragraph_entity->getSummaryCount(); + } } } + 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', [ + ->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 f40f4fa..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">1 Child | 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'); } /**