You are here

public function SvgImageFormatter::viewElements in Svg Image 8

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

File

src/Plugin/Field/FieldFormatter/SvgImageFormatter.php, line 74

Class

SvgImageFormatter
Plugin implementation of the 'image' formatter.

Namespace

Drupal\svg_image\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;
  }
  $imageStyleSetting = $this
    ->getSetting('image_style');

  // Collect cache tags to be added for each item in the field.
  $cacheTags = [];
  if (!empty($imageStyleSetting)) {
    $imageStyle = $this->imageStyleStorage
      ->load($imageStyleSetting);
    $cacheTags = $imageStyle ? $imageStyle
      ->getCacheTags() : [];
  }
  $svg_attributes = $this
    ->getSetting('svg_attributes');
  foreach ($files as $delta => $file) {
    $attributes = [];
    $isSvg = svg_image_is_file_svg($file);
    if ($isSvg) {
      $attributes = $svg_attributes;
    }
    $cacheContexts = [];
    if (isset($linkFile)) {
      $imageUri = $file
        ->getFileUri();
      $url = Url::fromUri(file_create_url($imageUri));
      $cacheContexts[] = 'url.site';
    }
    $cacheTags = Cache::mergeTags($cacheTags, $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 (isset($item->_attributes)) {
      $attributes += $item->_attributes;
    }
    unset($item->_attributes);
    $isSvg = svg_image_is_file_svg($file);
    if (!$isSvg || $this
      ->getSetting('svg_render_as_image')) {
      $elements[$delta] = [
        '#theme' => 'image_formatter',
        '#item' => $item,
        '#item_attributes' => $attributes,
        '#image_style' => $isSvg ? NULL : $imageStyleSetting,
        '#url' => $url,
        '#cache' => [
          'tags' => $cacheTags,
          'contexts' => $cacheContexts,
        ],
      ];
    }
    else {

      // Render as SVG tag.
      $svgRaw = $this
        ->fileGetContents($file);
      if ($svgRaw) {
        $svgRaw = preg_replace([
          '/<\\?xml.*\\?>/i',
          '/<!DOCTYPE((.|\\n|\\r)*?)">/i',
        ], '', $svgRaw);
        $svgRaw = trim($svgRaw);
        if ($url) {
          $elements[$delta] = [
            '#type' => 'link',
            '#url' => $url,
            '#title' => Markup::create($svgRaw),
            '#cache' => [
              'tags' => $cacheTags,
              'contexts' => $cacheContexts,
            ],
          ];
        }
        else {
          $elements[$delta] = [
            '#markup' => Markup::create($svgRaw),
            '#cache' => [
              'tags' => $cacheTags,
              'contexts' => $cacheContexts,
            ],
          ];
        }
      }
    }
  }
  return $elements;
}