Skip to content
Snippets Groups Projects

Issue #3257729: Formatters shouldn't repeat core code

Files
3
@@ -2,7 +2,6 @@
namespace Drupal\svg_image_responsive\Plugin\Field\FieldFormatter;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
@@ -59,48 +58,32 @@ class SvgResponsiveImageFormatter extends ResponsiveImageFormatter {
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
/** @var \Drupal\file\Entity\File[] $files */
$files = $this->getEntitiesToView($items, $langcode);
$elements = parent::viewElements($items, $langcode);
// Early opt-out if the field is empty.
if (empty($files)) {
if (empty($elements)) {
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.
/** @var \Drupal\file\Entity\File[] $files */
$files = $this->getEntitiesToView($items, $langcode);
$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 (!svg_image_is_file_svg($file)) {
// Simply keep the render array of the parent formatter for non-SVGs.
continue;
}
if ($isSvg) {
if ($this->getSetting('svg_render_as_image')) {
$attributes = $svgAttributes;
// Do not provide SVG dimensions when the width or height are already
// provided. Otherwise, do provide them when the image style is set.
@@ -113,49 +96,10 @@ class SvgResponsiveImageFormatter extends ResponsiveImageFormatter {
$attributes['width'] = $dimensions['width'];
$attributes['height'] = $dimensions['height'];
}
}
$cacheContexts = [];
// Link the <picture> element to the original file.
if (isset($linkFile)) {
$imageUri = $file->getFileUri();
$url = $this->fileUrlGenerator->generate($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);
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,
],
];
$elements[$delta]['#theme'] = 'image_formatter';
$elements[$delta]['#image_style'] = NULL;
$elements[$delta]['#item_attributes'] += $attributes;
}
else {
// Render as SVG tag.
@@ -164,14 +108,13 @@ class SvgResponsiveImageFormatter extends ResponsiveImageFormatter {
$svgRaw = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $svgRaw);
$svgRaw = trim($svgRaw);
if ($url) {
if ($elements[$delta]['#url']) {
$elements[$delta] = [
'#type' => 'link',
'#url' => $url,
'#url' => $elements[$delta]['#url'],
'#title' => Markup::create($svgRaw),
'#cache' => [
'tags' => $cacheTags,
'contexts' => $cacheContexts,
'tags' => $elements[$delta]['#cache']['tags'],
],
];
}
@@ -179,8 +122,7 @@ class SvgResponsiveImageFormatter extends ResponsiveImageFormatter {
$elements[$delta] = [
'#markup' => Markup::create($svgRaw),
'#cache' => [
'tags' => $cacheTags,
'contexts' => $cacheContexts,
'tags' => $elements[$delta]['#cache']['tags'],
],
];
}
Loading