You are here

public function BgImgFieldFormatter::viewElements in Background Image Field 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 ResponsiveImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/BgImgFieldFormatter.php, line 156

Class

BgImgFieldFormatter
Plugin implementation of the 'image' formatter.

Namespace

Drupal\bg_img_field\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $entity = $items
    ->getEntity();

  // Load the files to render.
  $files = [];
  foreach ($items
    ->getValue() as $item) {
    $files[] = [
      'file' => File::load($item['target_id']),
      'item' => $item,
    ];
  }

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  return $this
    ->buildElement($files, $entity);
}