You are here

public function FotoramaGalleryFormatter::viewElements in Fotorama Gallery 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/Field/FieldFormatter/FotoramaGalleryFormatter.php \Drupal\fotorama_gallery\Plugin\Field\FieldFormatter\FotoramaGalleryFormatter::viewElements()

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 ImageFormatter::viewElements

File

src/Plugin/Field/FieldFormatter/FotoramaGalleryFormatter.php, line 522

Class

FotoramaGalleryFormatter
Plugin implementation of the 'fotorama_gallery display' formatter.

Namespace

Drupal\fotorama_gallery\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $files = $this
    ->getEntitiesToView($items, $langcode);

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

  // Get all settings.
  $dimensions = $this
    ->getSettings()['dimensions'];
  $others = $this
    ->getSettings()['others'];
  $autoplay = $this
    ->getSettings()['autoplay'];
  $navigation = $this
    ->getSettings()['navigation'];
  $transition = $this
    ->getSettings()['transition'];
  $image_url = NULL;

  // Load image style storage object.
  $image_style_setting = $this
    ->getSetting('image_style');
  $image_style = $this->imageStyleStorage
    ->load($image_style_setting);

  // Check if thumbs option is enabled.
  $thumbs = $navigation['nav'] === 1;

  // Get thumb style storage.
  $thumb_style = FALSE;
  if ($thumbs) {
    $image_style_thumbs_setting = 'thumbnail';
    $thumb_style = $this->imageStyleStorage
      ->load($image_style_thumbs_setting);
  }

  // Collect cache tags to be added for each item in the field.
  $base_cache_tags = [];
  if ($image_style) {
    $base_cache_tags = $image_style
      ->getCacheTags();
  }
  if ($thumb_style) {
    $base_cache_tags = $thumb_style
      ->getCacheTags();
  }
  foreach ($files as $delta => $file) {
    $cache_contexts = [];
    $image_uri = $file
      ->getFileUri();
    $url = $image_style ? $image_style
      ->buildUrl($image_uri) : file_create_url($image_uri);
    $image_url = URL::fromUri($url);

    // Generate Thumbs.
    if ($thumbs) {
      $thumb_uri = $thumb_style
        ->buildUrl($image_uri);
      $thumb_url = URL::fromUri($thumb_uri)
        ->toString();
      $thumb = [
        'data-thumb' => $thumb_url,
      ];
    }
    $cache_contexts[] = 'url.site';
    $cache_tags = Cache::mergeTags($base_cache_tags, $file
      ->getCacheTags());
    $elements[$delta] = [
      '#type' => 'link',
      '#title' => '',
      '#url' => $image_url,
      '#attributes' => isset($thumb) ? $thumb : [],
      '#cache' => [
        'tags' => $cache_tags,
        'contexts' => $cache_contexts,
      ],
    ];
  }

  // Add custom settings to the Fotorama gallery.
  $elements['#theme'] = 'fotorama_gallery_field';
  if (!empty($dimensions['ratio'])) {
    $elements['attributes']['data-ratio'] = $dimensions['ratio'];
  }
  if (!empty($dimensions['width'])) {
    $elements['attributes']['data-width'] = $dimensions['width'];
  }
  if (!empty($dimensions['maxwidth'])) {
    $elements['attributes']['data-maxwidth'] = $dimensions['maxwidth'];
  }
  if (!empty($dimensions['minwidth'])) {
    $elements['attributes']['data-minwidth'] = $dimensions['minwidth'];
  }
  if (!empty($dimensions['height'])) {
    $elements['attributes']['data-height'] = $dimensions['height'];
  }
  if (!empty($dimensions['maxheight'])) {
    $elements['attributes']['data-maxheight'] = $dimensions['maxheight'];
  }
  if (!empty($dimensions['minheight'])) {
    $elements['attributes']['data-minheight'] = $dimensions['minheight'];
  }
  if (!empty($others['fit'])) {
    $elements['attributes']['data-fit'] = $this->fitOptions[$others['fit']];
  }
  if (!empty($others['allowfullscreen']) && $others['allowfullscreen']) {
    $elements['attributes']['data-allowfullscreen'] = $this->allowFullScreenOptions[$others['allowfullscreen']];
  }
  $elements['attributes']['data-loop'] = $others['loop'] ? 'true' : 'false';
  $elements['attributes']['data-shuffle'] = $others['shuffle'] ? 'true' : 'false';
  $elements['attributes']['data-keyboard'] = $others['keyboard'] ? 'true' : 'false';
  if (!empty($others['arrows'])) {
    $elements['attributes']['data-arrows'] = $this->arrowsOptions[$others['arrows']];
  }
  $elements['attributes']['data-click'] = $others['click'] ? 'true' : 'false';
  $elements['attributes']['data-swipe'] = $others['swipe'] ? 'true' : 'false';
  $elements['attributes']['data-trackpad'] = $others['trackpad'] ? 'true' : 'false';
  $elements['attributes']['data-stopautoplayontouch'] = $autoplay['stopautoplayontouch'] ? 'true' : 'false';
  if (!empty($navigation['nav'])) {
    $elements['attributes']['data-nav'] = $this->navOptions[$navigation['nav']];
  }
  if (!empty($navigation['navposition'])) {
    $elements['attributes']['data-navposition'] = $this->navPositionOptions[$navigation['navposition']];
  }
  if (!empty($transition['transition'])) {
    $elements['attributes']['data-transition'] = $this->transitionOptions[$transition['transition']];
  }
  if (!empty($transition['clicktransition'])) {
    $elements['attributes']['data-clicktransition'] = $this->clickTransitionOptions[$transition['clicktransition']];
  }
  return $elements;
}