You are here

public function ImageSizesFormatter::viewElements in Picture 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/ImageSizesFormatter.php, line 128
Contains \Drupal\picture\Plugin\field\formatter\ImageSizesFormatter.

Class

ImageSizesFormatter
Plugin for image with sizes attribute formatter.

Namespace

Drupal\picture\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items) {
  $elements = array();

  // Check if the formatter involves a link.
  $image_link_setting = $this
    ->getSetting('image_link');
  if ($image_link_setting == 'content') {
    $entity = $items
      ->getEntity();
    if (!$entity
      ->isNew()) {

      // @todo Remove when theme_image_formatter() has support for route name.
      $uri['path'] = $entity
        ->getSystemPath();
      $uri['options'] = $entity
        ->urlInfo()
        ->getOptions();
    }
  }
  elseif ($image_link_setting == 'file') {
    $link_file = TRUE;
  }
  $fallback_image_style = '';

  // Check if the user defined a custom fallback image style.
  if ($this
    ->getSetting('fallback_image_style')) {
    $fallback_image_style = $this
      ->getSetting('fallback_image_style');
  }

  // Collect cache tags to be added for each item in the field.
  $image_styles_to_load = array_filter($this
    ->getSetting('image_styles'));
  if ($fallback_image_style) {
    $image_styles_to_load[] = $fallback_image_style;
  }
  $image_styles = ImageStyle::loadMultiple($image_styles_to_load);
  foreach ($image_styles as $image_style) {
    $all_cache_tags[] = $image_style
      ->getCacheTag();
  }
  $cache_tags = NestedArray::mergeDeepArray($all_cache_tags);
  foreach ($items as $delta => $item) {
    if ($item->entity) {
      if (isset($link_file)) {
        $image_uri = $item->entity
          ->getFileUri();
        $uri = array(
          'path' => file_create_url($image_uri),
          'options' => array(),
        );
      }

      // 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_attributes = $item->_attributes;
      unset($item->_attributes);
      $elements[$delta] = array(
        '#theme' => 'image_sizes_formatter',
        '#attached' => array(
          'library' => array(
            'core/picturefill',
          ),
        ),
        '#item' => $item,
        '#item_attributes' => $item_attributes,
        '#image_styles' => array_filter($this
          ->getSetting('image_styles')),
        '#fallback_image_style' => $this
          ->getSetting('fallback_image_style'),
        '#sizes' => $this
          ->getSetting('sizes'),
        '#path' => isset($uri) ? $uri : '',
        '#cache' => array(
          'tags' => $cache_tags,
        ),
      );
    }
  }
  return $elements;
}