You are here

public function ImageZoomFieldFormatter::viewElements in Image Field Zoom 8

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

File

src/Plugin/Field/FieldFormatter/ImageZoomFieldFormatter.php, line 209
Contains Drupal\imagefield_zoom\Plugin\Field\FieldFormatter\ImageZoomFieldFormatter.

Class

ImageZoomFieldFormatter
Plugin implementation of the 'imagezoom_field_formatter' formatter.

Namespace

Drupal\imagefield_zoom\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = array();
  $files = $this
    ->getEntitiesToView($items, $langcode);
  $images = array();
  $original_urls = array();

  // Early opt-out if the field is empty.
  if (empty($files)) {
    return $elements;
  }
  $image_style_setting = $this
    ->getSetting('image_style');

  // Collect cache tags to be added for each item in the field.
  $cache_tags = array();
  if (!empty($image_style_setting)) {
    $image_style = $this->imageStyleStorage
      ->load($image_style_setting);
    $cache_tags = $image_style
      ->getCacheTags();
  }
  foreach ($files as $delta => $file) {
    $cache_contexts = array();
    $image_uri = $file
      ->getFileUri();
    $cache_contexts[] = 'url.site';
    $cache_tags = Cache::mergeTags($cache_tags, $file
      ->getCacheTags());

    // Extract field item attributes for the theme function, and unset them
    // from the $item so that the field template does not re-render them.
    $item = $file->_referringItem;
    if (!empty($item->_attributes)) {
      $item_attributes = $item->_attributes;
    }
    $image_target_id = $item
      ->getValue()['target_id'];
    $image_uri = file_create_url($image_uri);
    $image_uri = parse_url($image_uri);
    $original_urls[$image_target_id] = $image_uri['path'];

    // Adding custom attributes to the img.
    $item_attributes['class'][] = 'original-image';

    // $item_attributes['width'] = '400';.
    $item_attributes['fid'] = $image_target_id;
    unset($item->_attributes);
    $images[$delta] = array(
      '#theme' => 'image_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#image_style' => $image_style_setting,
      '#prefix' => '<span class="image-zoom">',
      '#suffix' => '</span>',
      '#cache' => array(
        'tags' => $cache_tags,
        'contexts' => $cache_contexts,
      ),
    );
  }
  return array(
    '#theme' => 'imagefield_zoom',
    '#images' => render($images),
    '#attached' => [
      'drupalSettings' => [
        'imageFieldZoom' => [
          'image_zoom_style' => $this
            ->getSetting('image_zoom_style'),
          'image_touchscreen_compatible' => $this
            ->getSetting('image_touchscreen_compatible'),
          'image_magnify' => $this
            ->getSetting('image_magnify'),
          'image_fade_duration' => $this
            ->getSetting('image_fade_duration'),
          'image_urls' => $original_urls,
        ],
      ],
    ],
  );
}