public function ParagraphsSmartTrimFormatter::viewElements in Paragraphs Trimmed 8
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides EntityReferenceRevisionsEntityFormatter::viewElements
File
- modules/
paragraphs_smart_trim/ src/ Plugin/ Field/ FieldFormatter/ ParagraphsSmartTrimFormatter.php, line 51
Class
- ParagraphsSmartTrimFormatter
- Plugin implementation of the 'paragraphs_trimmed_formatter' formatter.
Namespace
Drupal\paragraphs_smart_trim\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
// If we have a summary field, just render it and send it out
if ($this
->getSummaryFieldValue($items)) {
return $this
->getSummaryFieldElement($items);
}
// Render the paragraphs output
$elements = parent::viewElements($items, $langcode);
$output = \Drupal::service('renderer')
->render($elements);
$element = [
'#type' => 'processed_text',
'#text' => $output,
'#format' => $this
->getSetting('format'),
'#langcode' => $langcode,
];
// Process text using formatter
$output = \Drupal::service('renderer')
->render($element);
// Create a basic text field item list
$definition = \Drupal::typedDataManager()
->createListDataDefinition('field_item:text');
$text_items = \Drupal::typedDataManager()
->create($definition, [
$output,
], NULL, $items
->getEntity()
->getTypedData());
// Set the text format of our new text field item list item and process
// using the smart trim formatter viewElements method with our settings.
$text_items->format = $this
->getSetting('format');
return $this->formatter
->setSettings($this
->getSettings())
->viewElements($text_items, $langcode);
}