You are here

public function SvgResponsiveImageFormatter::viewElements in Svg Image 8

Same name and namespace in other branches
  1. 2.x modules/svg_image_responsive/src/Plugin/Field/FieldFormatter/SvgResponsiveImageFormatter.php \Drupal\svg_image_responsive\Plugin\Field\FieldFormatter\SvgResponsiveImageFormatter::viewElements()
  2. 1.x modules/svg_image_responsive/src/Plugin/Field/FieldFormatter/SvgResponsiveImageFormatter.php \Drupal\svg_image_responsive\Plugin\Field\FieldFormatter\SvgResponsiveImageFormatter::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 ResponsiveImageFormatter::viewElements

File

modules/svg_image_responsive/src/Plugin/Field/FieldFormatter/SvgResponsiveImageFormatter.php, line 76

Class

SvgResponsiveImageFormatter
Plugin implementation of the 'responsive_image' formatter.

Namespace

Drupal\svg_image_responsive\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  /** @var \Drupal\file\Entity\File[] $files */
  $files = $this
    ->getEntitiesToView($items, $langcode);

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

  // Check if the formatter involves a link.
  if ($imageLinkSetting === 'content') {
    $entity = $items
      ->getEntity();
    if (!$entity
      ->isNew()) {
      $url = $entity
        ->toUrl();
    }
  }
  elseif ($imageLinkSetting === 'file') {
    $linkFile = TRUE;
  }

  // Collect cache tags to be added for each item in the field.
  $responsiveImageStyle = $this->responsiveImageStyleStorage
    ->load($this
    ->getSetting('responsive_image_style'));
  $imageStylesToLoad = [];
  $cacheTags = [];
  if ($responsiveImageStyle) {
    $cacheTags = Cache::mergeTags($cacheTags, $responsiveImageStyle
      ->getCacheTags());
    $imageStylesToLoad = $responsiveImageStyle
      ->getImageStyleIds();
  }
  $imageStyles = $this->imageStyleStorage
    ->loadMultiple($imageStylesToLoad);
  foreach ($imageStyles as $image_style) {
    $cacheTags = Cache::mergeTags($cacheTags, $image_style
      ->getCacheTags());
  }
  $svgAttributes = $this
    ->getSetting('svg_attributes');
  foreach ($files as $delta => $file) {
    $attributes = [];
    $isSvg = svg_image_is_file_svg($file);
    if ($isSvg) {
      $attributes = $svgAttributes;
    }
    $cacheContexts = [];
    if (isset($linkFile)) {
      $imageUri = $file
        ->getFileUri();
      $url = Url::fromUri(file_create_url($imageUri));
      $cacheContexts[] = 'url.site';
    }
    $cacheTags = Cache::mergeTags($cacheTags, $file
      ->getCacheTags());

    // Link the <picture> element to the original file.
    if (isset($linkFile)) {
      $url = file_url_transform_relative(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;
    if (isset($item->_attributes)) {
      $attributes += $item->_attributes;
    }
    unset($item->_attributes);
    if (!$isSvg) {
      $elements[$delta] = [
        '#theme' => 'responsive_image_formatter',
        '#item' => $item,
        '#item_attributes' => $attributes,
        '#responsive_image_style_id' => $responsiveImageStyle ? $responsiveImageStyle
          ->id() : '',
        '#url' => $url,
        '#cache' => [
          'tags' => $cacheTags,
        ],
      ];
    }
    elseif ($this
      ->getSetting('svg_render_as_image')) {
      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#item_attributes' => $attributes,
        '#image_style' => NULL,
        '#url' => $url,
        '#cache' => [
          'tags' => $cacheTags,
          'contexts' => $cacheContexts,
        ],
      ];
    }
    else {

      // Render as SVG tag.
      $svgRaw = $this
        ->fileGetContents($file);
      if ($svgRaw) {
        $svgRaw = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $svgRaw);
        $svgRaw = trim($svgRaw);
        $elements[$delta] = [
          '#markup' => Markup::create($svgRaw),
          '#cache' => [
            'tags' => $cacheTags,
            'contexts' => $cacheContexts,
          ],
        ];
      }
    }
  }
  return $elements;
}