You are here

public function GridStackFileFormatterBase::buildElements in GridStack 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/GridStackFileFormatterBase.php \Drupal\gridstack\Plugin\Field\FieldFormatter\GridStackFileFormatterBase::buildElements()

Build the gridstack carousel elements.

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

File

src/Plugin/Field/FieldFormatter/GridStackFileFormatterBase.php, line 96

Class

GridStackFileFormatterBase
Base class for gridstack image and file ER formatters.

Namespace

Drupal\gridstack\Plugin\Field\FieldFormatter

Code

public function buildElements(array &$build, $files) {
  $settings =& $build['settings'];
  $item_id = $settings['item_id'];
  $media = method_exists($this, 'getMediaItem');
  foreach ($files as $delta => $file) {
    $settings['delta'] = $delta;
    $settings['type'] = 'image';

    /** @var Drupal\image\Plugin\Field\FieldType\ImageItem $item */
    $item = $file->_referringItem;
    $settings['file_tags'] = $file
      ->getCacheTags();
    $settings['uri'] = $file
      ->getFileUri();

    // Overrides fallback breakpoint image_style with grid image_style.
    // This tells theme_blazy() to respect different image style per item.
    if (!empty($settings['breakpoints'])) {
      foreach ($settings['breakpoints'] as $key => &$breakpoint) {
        if (isset($breakpoint['image_style']) && !empty($breakpoint['grids'][$delta]) && !empty($breakpoint['grids'][$delta]['image_style'])) {
          $breakpoint['image_style'] = $breakpoint['grids'][$delta]['image_style'];
        }

        // Overrides image style to use a defined image style per grid item.
        // This allows each individual box to have different image style.
        if ($key == 'lg' && !empty($breakpoint['grids'][$delta]['image_style'])) {
          $settings['_dimensions'] = empty($settings['background']);
          $settings['image_style'] = $breakpoint['grids'][$delta]['image_style'];
        }
      }
    }
    $element = [
      'item' => $item,
      'settings' => $settings,
    ];

    // If imported Drupal\blazy\Dejavu\BlazyVideoTrait.
    if ($media) {
      if (!empty($this
        ->getImageItem($item))) {
        $element['item'] = $this
          ->getImageItem($item)['item'];
        $element['settings'] = array_merge($settings, $this
          ->getImageItem($item)['settings']);
      }
      $this
        ->getMediaItem($element, $file);
    }

    // Image with responsive image, lazyLoad, and lightbox supports.
    $element[$item_id] = $this->formatter
      ->getImage($element);
    if (!empty($settings['caption'])) {
      foreach ($settings['caption'] as $caption) {
        $element['caption'][$caption] = empty($element['item']->{$caption}) ? [] : [
          '#markup' => Xss::filterAdmin($element['item']->{$caption}),
        ];
      }
    }

    // Build individual gridstack item.
    $build['items'][$delta] = $element;
    unset($element);
  }
}