You are here

public function ResponsiveImageFormatter::viewElements in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::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 FormatterInterface::viewElements

1 call to ResponsiveImageFormatter::viewElements()
ResponsiveImageTestFormatter::viewElements in core/modules/responsive_image/tests/modules/responsive_image_test_module/src/Plugin/Field/FieldFormatter/ResponsiveImageTestFormatter.php
Builds a renderable array for a field value.
1 method overrides ResponsiveImageFormatter::viewElements()
ResponsiveImageTestFormatter::viewElements in core/modules/responsive_image/tests/modules/responsive_image_test_module/src/Plugin/Field/FieldFormatter/ResponsiveImageTestFormatter.php
Builds a renderable array for a field value.

File

core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php, line 195
Contains \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter.

Class

ResponsiveImageFormatter
Plugin for responsive image formatter.

Namespace

Drupal\responsive_image\Plugin\Field\FieldFormatter

Code

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

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

  // Check if the formatter involves a link.
  if ($this
    ->getSetting('image_link') == 'content') {
    $entity = $items
      ->getEntity();
    if (!$entity
      ->isNew()) {
      $url = $entity
        ->urlInfo();
    }
  }
  elseif ($this
    ->getSetting('image_link') == 'file') {
    $link_file = TRUE;
  }

  // Collect cache tags to be added for each item in the field.
  $responsive_image_style = $this->responsiveImageStyleStorage
    ->load($this
    ->getSetting('responsive_image_style'));
  $image_styles_to_load = array();
  $cache_tags = [];
  if ($responsive_image_style) {
    $cache_tags = Cache::mergeTags($cache_tags, $responsive_image_style
      ->getCacheTags());
    $image_styles_to_load = $responsive_image_style
      ->getImageStyleIds();
  }
  $image_styles = $this->imageStyleStorage
    ->loadMultiple($image_styles_to_load);
  foreach ($image_styles as $image_style) {
    $cache_tags = Cache::mergeTags($cache_tags, $image_style
      ->getCacheTags());
  }
  foreach ($files as $delta => $file) {

    // Link the <picture> element to the original file.
    if (isset($link_file)) {
      $url = Url::fromUri(file_create_url($file
        ->getFileUri()));
    }

    // 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;
    $item_attributes = $item->_attributes;
    unset($item->_attributes);
    $elements[$delta] = array(
      '#theme' => 'responsive_image_formatter',
      '#item' => $item,
      '#item_attributes' => $item_attributes,
      '#responsive_image_style_id' => $responsive_image_style ? $responsive_image_style
        ->id() : '',
      '#url' => $url,
      '#cache' => array(
        'tags' => $cache_tags,
      ),
    );
  }
  return $elements;
}