You are here

public function Paragraph::getTextSummary in Paragraphs 8

Returns summary for all text type paragraph.

Parameters

string $field_name: Field definition id for paragraph.

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: Field definition for paragraph.

Return value

string Short summary for text paragraph.

1 call to Paragraph::getTextSummary()
Paragraph::getSummaryItems in src/Entity/Paragraph.php
Returns the summary items of the Paragraph.

File

src/Entity/Paragraph.php, line 722

Class

Paragraph
Defines the Paragraph entity.

Namespace

Drupal\paragraphs\Entity

Code

public function getTextSummary($field_name, FieldDefinitionInterface $field_definition) {
  $text_types = [
    'text_with_summary',
    'text',
    'text_long',
    'list_string',
    'string',
  ];
  $excluded_text_types = [
    'parent_id',
    'parent_type',
    'parent_field_name',
  ];
  $summary = '';
  if (in_array($field_definition
    ->getType(), $text_types)) {
    if (in_array($field_name, $excluded_text_types)) {
      return '';
    }
    $text = $this
      ->get($field_name)->value;
    $summary = Unicode::truncate(trim(strip_tags($text)), 150);
    if (empty($summary)) {

      // Autoescape is applied to the summary when it is rendered with twig,
      // make it a Markup object so HTML tags are displayed correctly.
      $summary = Markup::create(Unicode::truncate(htmlspecialchars(trim($text)), 150));
    }
  }
  return $summary;
}