You are here

public function BlazyFormatterBlazy::viewElements in Blazy 7

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/BlazyFormatterBlazy.php \Drupal\blazy\Plugin\Field\FieldFormatter\BlazyFormatterBlazy::viewElements()

Implements hook_field_formatter_view().

Overrides FormatterBase::viewElements

File

src/Plugin/Field/FieldFormatter/BlazyFormatterBlazy.php, line 37

Class

BlazyFormatterBlazy
Plugin implementation of the `Blazy File` or `Blazy Image` for Blazy only.

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

public function viewElements($items, $entity) {
  $build = [];
  $files = $this
    ->getEntitiesToView($items);

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $build;
  }

  // Collects specific settings to this formatter.
  $settings = $this
    ->buildSettings();
  $settings['blazy'] = TRUE;
  $settings['namespace'] = $settings['item_id'] = $settings['lazy'] = 'blazy';

  // Build the settings.
  $build = [
    'settings' => $settings,
  ];

  // Modifies settings before building elements.
  $this->formatter
    ->preBuildElements($build, $files, $entity);

  // Build the elements.
  $this
    ->buildElements($build, $files);

  // Modifies settings post building elements.
  $this->formatter
    ->postBuildElements($build, $files, $entity);

  // Pass to manager for easy updates to all Blazy formatters.
  return $this->formatter
    ->build($build);
}