You are here

public function GridStackFileFormatterBase::buildElements in GridStack 8.2

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

Build the gridstack carousel elements.

File

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

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'];
  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();
    $element = [
      'item' => $item,
      'settings' => $settings,
    ];

    // If imported Drupal\blazy\Dejavu\BlazyVideoTrait.
    $this
      ->buildElement($element, $file);

    // Image with responsive image, lazyLoad, and lightbox supports.
    $element[$item_id] = $this->formatter
      ->getBlazy($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);
  }
}