You are here

public function BlazyFormatterBlazy::buildElements 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::buildElements()

Build the Blazy elements for image and file entity/ media.

1 call to BlazyFormatterBlazy::buildElements()
BlazyFormatterBlazy::viewElements in src/Plugin/Field/FieldFormatter/BlazyFormatterBlazy.php
Implements hook_field_formatter_view().

File

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

Class

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

Namespace

Drupal\blazy\Plugin\Field\FieldFormatter

Code

public function buildElements(array &$build, $files) {
  $settings = $build['settings'];
  foreach ($files as $delta => $item) {

    // Some settings need to be available before arriving at theme_blazy() so
    // that lightboxes and media switcher can get context to work with.
    // The trouble is they are not always available depending on file entity
    // and other supported modules availability which is not always there.
    $settings['delta'] = $delta;
    $settings['type'] = isset($item->type) ? $item->type : 'image';
    $settings['uri'] = isset($item->uri) ? $item->uri : '';
    $box['item'] = $item;
    $box['settings'] = $settings;
    $this
      ->buildElement($box, $item, $delta);

    // Build caption if so configured.
    if (!empty($settings['caption'])) {
      foreach ($settings['caption'] as $caption) {
        if (isset($item->{$caption}) && ($caption_content = array_filter($this
          ->getCaption($item, $caption, $settings)))) {
          $box['captions'][$caption] = $caption_content;
        }
      }
    }

    // Image with grid, responsive image, lazyLoad, and lightbox supports.
    $build[$delta] = $this
      ->formatter()
      ->getBlazy($box);
    unset($box);
  }
}