function ResponsiveImageFormatter::viewElements

Same name and namespace in other branches
  1. 8.9.x core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::viewElements()
  2. 10 core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::viewElements()
  3. 11.x core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::viewElements()

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

Class

ResponsiveImageFormatter
Plugin for responsive image formatter.

Namespace

Drupal\responsive_image\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;
    }
    $url = NULL;
    // Check if the formatter involves a link.
    if ($this->getSetting('image_link') == 'content') {
        $entity = $items->getEntity();
        if (!$entity->isNew()) {
            $url = $entity->toUrl();
        }
    }
    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 = [];
    $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) {
        assert($file instanceof FileInterface);
        // Link the <picture> element to the original file.
        if (isset($link_file)) {
            $url = $file->createFileUrl();
        }
        // 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] = [
            '#theme' => 'responsive_image_formatter',
            '#item' => $item,
            '#item_attributes' => $item_attributes,
            '#responsive_image_style_id' => $responsive_image_style ? $responsive_image_style->id() : '',
            '#url' => $url,
            '#cache' => [
                'tags' => $cache_tags,
            ],
        ];
    }
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.