diff --git a/src/Entity/Paragraph.php b/src/Entity/Paragraph.php index 5f588cb..cb61d8a 100644 --- a/src/Entity/Paragraph.php +++ b/src/Entity/Paragraph.php @@ -3,10 +3,12 @@ namespace Drupal\paragraphs\Entity; use Drupal\Component\Utility\NestedArray; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\EntityTypeInterface; +use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\TypedData\TranslatableInterface; use Drupal\field\Entity\FieldStorageConfig; use Drupal\entity_reference_revisions\EntityNeedsSaveInterface; @@ -429,4 +431,142 @@ class Paragraph extends ContentEntityBase implements ParagraphInterface, EntityN return $duplicate; } + /** + * {@inheritdoc} + */ + public function getSummary() { + $summary = []; + 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 != '') { + $summary[] = $file_summary; + } + } + + $text_summary = $this->getTextSummary($field_name, $field_definition); + if ($text_summary != '') { + $summary[] = $text_summary; + } + + if ($field_definition->getType() == 'entity_reference_revisions') { + $nested_summary = $this->getNestedSummary($field_name); + if ($nested_summary != '') { + $summary[] = $nested_summary; + } + } + + if ($field_type = $field_definition->getType() == 'entity_reference') { + if (!in_array($field_name, ['type', 'uid', 'revision_uid'])) { + if ($this->get($field_name)->entity) { + $summary[] = $this->get($field_name)->entity->label(); + } + } + } + + } + + $paragraphs_type = $this->getParagraphType(); + foreach ($paragraphs_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin) { + if ($plugin_summary = $plugin->settingsSummary($this)) { + $summary = array_merge($summary, $plugin_summary); + } + } + + $collapsed_summary_text = implode(', ', $summary); + return strip_tags($collapsed_summary_text); + } + + /** + * Returns summary for file paragraph. + * + * @param string $field_name + * Field name from field definition. + * + * @return string + * Summary for image. + */ + protected function getFileSummary($field_name) { + $summary = ''; + if ($this->get($field_name)->entity) { + foreach ($this->get($field_name) as $file_key => $file_value) { + + $text = ''; + if ($file_value->description != '') { + $text = $file_value->description; + } + elseif ($file_value->title != '') { + $text = $file_value->title; + } + elseif ($file_value->alt != '') { + $text = $file_value->alt; + } + elseif ($file_value->entity->getFileName()) { + $text = $file_value->entity->getFileName(); + } + + if (strlen($text) > 150) { + $text = Unicode::truncate($text, 150); + } + + $summary = $text; + } + } + + return $summary; + } + + /** + * Returns summary for nested paragraphs. + * + * @param string $field_name + * Field definition id for paragraph. + * + * @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(); + } + } + + return $summary; + } + + /** + * Returns summary for all text type paragraph. + * + * @param string $field_name + * Field definition id for paragraph. + * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition + * Field definition for paragraph. + * + * @return string + * Short summary for text paragraph. + */ + public function getTextSummary($field_name, FieldDefinitionInterface $field_definition) { + $text_types = [ + 'text_with_summary', + 'text', + 'text_long', + 'list_string', + ]; + + $summary = ''; + if (in_array($field_definition->getType(), $text_types)) { + $text = $this->get($field_name)->value; + if (strlen($text) > 150) { + $text = Unicode::truncate($text, 150); + } + + $summary = $text; + } + + return $summary; + } + } diff --git a/src/ParagraphInterface.php b/src/ParagraphInterface.php index 5d60881..5cdbcc8 100644 --- a/src/ParagraphInterface.php +++ b/src/ParagraphInterface.php @@ -21,4 +21,13 @@ interface ParagraphInterface extends ContentEntityInterface, EntityOwnerInterfac * The parent entity. */ public function getParentEntity(); + + /** + * Returns short summary for paragraph. + * + * @return string + * The text without tags. + */ + public function getSummary(); + } diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 1402b5f..0f756ef 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -666,7 +666,7 @@ class InlineParagraphsWidget extends WidgetBase { $element['subform'] = array(); $element['behavior_plugins'] = []; if ($paragraphs_entity) { - $summary = $this->addCollapsedSummary($paragraphs_entity); + $summary = $paragraphs_entity->getSummary(); $element['top']['paragraph_summary']['fields_info'] = [ '#markup' => $summary, '#prefix' => '
', @@ -1401,53 +1401,6 @@ class InlineParagraphsWidget extends WidgetBase { } /** - * @param \Drupal\paragraphs\Entity\Paragraph $paragraphs_entity - * Entity where to extract the values. - * - * @return string $collapsed_summary_text - * The text without tags to return. - */ - public function addCollapsedSummary(paragraphs\Entity\Paragraph $paragraphs_entity) { - $text_types = ['text_with_summary', 'text', 'text_long', 'list_string']; - $summary = []; - foreach ($paragraphs_entity->getFieldDefinitions() as $key => $value) { - if ($value->getType() == 'image') { - if ($paragraphs_entity->get($key)->entity) { - foreach ($paragraphs_entity->get($key) as $image_key => $image_value) { - if ($image_value->title != '') { - $text = $image_value->title; - } - elseif ($image_value->alt != '') { - $text = $image_value->alt; - } - elseif ($text = $image_value->entity->filename->value) { - $text = $image_value->entity->filename->value; - } - if (strlen($text) > 50) { - $text = strip_tags(substr($text, 0, 150)); - } - $summary[] = $text; - } - } - } - if (in_array($value->getType(), $text_types)) { - $text = $paragraphs_entity->get($key)->value; - if (strlen($text) > 50) { - $text = strip_tags(substr($text, 0, 150)); - } - $summary[] = $text; - } - if ($field_type = $value->getType() == 'entity_reference_revisions') { - if ($paragraphs_entity->get($key) && $paragraphs_entity->get($key)->entity) { - $summary[] = $this->addCollapsedSummary($paragraphs_entity->get($key)->entity); - } - } - } - $collapsed_summary_text = implode(', ', $summary); - return strip_tags($collapsed_summary_text); - } - - /** * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) { diff --git a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php index 78fec4d..097984a 100644 --- a/src/Plugin/Field/FieldWidget/ParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/ParagraphsWidget.php @@ -656,7 +656,7 @@ class ParagraphsWidget extends WidgetBase { $element['subform'] = array(); $element['behavior_plugins'] = []; if ($paragraphs_entity) { - $summary = $this->addCollapsedSummary($paragraphs_entity); + $summary = $paragraphs_entity->getSummary(); $element['top']['paragraph_summary']['fields_info'] = [ '#markup' => $summary, '#prefix' => '
', @@ -1414,66 +1414,6 @@ class ParagraphsWidget extends WidgetBase { } /** - * @param \Drupal\paragraphs\Entity\Paragraph $paragraphs_entity - * Entity where to extract the values. - * - * @return string $collapsed_summary_text - * The text without tags to return. - */ - public function addCollapsedSummary(paragraphs\Entity\Paragraph $paragraphs_entity) { - $text_types = ['text_with_summary', 'text', 'text_long', 'list_string']; - $summary = []; - foreach ($paragraphs_entity->getFieldDefinitions() as $key => $value) { - if ($value->getType() == 'image') { - if ($paragraphs_entity->get($key)->entity) { - foreach ($paragraphs_entity->get($key) as $image_key => $image_value) { - if ($image_value->title != '') { - $text = $image_value->title; - } - elseif ($image_value->alt != '') { - $text = $image_value->alt; - } - elseif ($text = $image_value->entity->filename->value) { - $text = $image_value->entity->filename->value; - } - if (strlen($text) > 50) { - $text = strip_tags(substr($text, 0, 150)); - } - $summary[] = $text; - } - } - } - if (in_array($value->getType(), $text_types)) { - $text = $paragraphs_entity->get($key)->value; - if (strlen($text) > 50) { - $text = strip_tags(substr($text, 0, 150)); - } - $summary[] = $text; - } - if ($field_type = $value->getType() == 'entity_reference_revisions') { - if ($paragraphs_entity->get($key) && $paragraphs_entity->get($key)->entity) { - $summary[] = $this->addCollapsedSummary($paragraphs_entity->get($key)->entity); - } - } - if ($field_type = $value->getType() == 'entity_reference') { - if (!in_array($key, ['type', 'uid', 'revision_uid'])) { - if ($paragraphs_entity->get($key)->entity) { - $summary[] = $paragraphs_entity->get($key)->entity->label(); - } - } - } - } - $paragraphs_type = $paragraphs_entity->getParagraphType(); - foreach ($paragraphs_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin) { - if ($plugin_summary = $plugin->settingsSummary($paragraphs_entity)) { - $summary = array_merge($summary, $plugin_summary); - } - } - $collapsed_summary_text = implode(', ', $summary); - return strip_tags($collapsed_summary_text); - } - - /** * {@inheritdoc} */ public static function isApplicable(FieldDefinitionInterface $field_definition) {