You are here

public function ParagraphsTrimmedFormatter::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

src/Plugin/Field/FieldFormatter/ParagraphsTrimmedFormatter.php, line 32

Class

ParagraphsTrimmedFormatter
Plugin implementation of the 'paragraphs_trimmed' formatter.

Namespace

Drupal\paragraphs_trimmed\Plugin\Field\FieldFormatter

Code

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);

  // 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);
}