public function BlazyTestEntityReferenceFormatterTest::viewElements in Blazy 8
Same name and namespace in other branches
- 8.2 tests/modules/blazy_test/src/Plugin/Field/FieldFormatter/BlazyTestEntityReferenceFormatterTest.php \Drupal\blazy_test\Plugin\Field\FieldFormatter\BlazyTestEntityReferenceFormatterTest::viewElements()
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 FormatterInterface::viewElements
File
- tests/modules/ blazy_test/ src/ Plugin/ Field/ FieldFormatter/ BlazyTestEntityReferenceFormatterTest.php, line 97 
Class
- BlazyTestEntityReferenceFormatterTest
- Plugin implementation of the 'Blazy Entity Reference' formatter.
Namespace
Drupal\blazy_test\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
  $entities = $this
    ->getEntitiesToView($items, $langcode);
  // Early opt-out if the field is empty.
  if (empty($entities)) {
    return [];
  }
  // Collects specific settings to this formatter.
  $settings = $this
    ->buildSettings();
  $build = [
    'settings' => $settings,
  ];
  $this->formatter
    ->buildSettings($build, $items);
  // Build the elements.
  $this
    ->buildElements($build, $entities, $langcode);
  // Alternatively use grid: BlazyGrid::build($build['items'], $settings).
  $elements = $build['items'];
  $elements['#attached'] = $this->formatter
    ->attach($settings);
  return $elements;
}