Returns summary for all text type paragraph.
string $field_name: Field definition id for paragraph.
\Drupal\Core\Field\FieldDefinitionInterface $field_definition: Field definition for paragraph.
string Short summary for text paragraph.
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(html_entity_decode(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;
}