public function SvgFormatter::viewElements in SVG Formatter 8
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
File
- src/
Plugin/ Field/ FieldFormatter/ SvgFormatter.php, line 246
Class
- SvgFormatter
- Plugin implementation of the 'svg_formatter' formatter.
Namespace
Drupal\svg_formatter\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$attributes = [];
if ($this
->getSetting('apply_dimensions')) {
$attributes['width'] = $this
->getSetting('width');
$attributes['height'] = $this
->getSetting('height');
}
foreach ($items as $delta => $item) {
if ($item->entity) {
$file = $item->entity;
$parent = $items
->getParent()
->getEntity();
// Skip if this is not a SVG image.
if ($item->entity
->getMimeType() !== 'image/svg+xml') {
continue;
}
$filename = $item->entity
->getFilename();
$default_alt = $this
->generateAltAttribute($filename);
$token_data = [
'file' => $this->entityRepository
->getTranslationFromContext($file),
$parent
->getEntityTypeId() => $this->entityRepository
->getTranslationFromContext($parent),
];
$replace_options = [
'clear' => TRUE,
];
if ($this
->getSetting('enable_alt')) {
if ($this
->getSetting('alt_string')) {
if ($alt = $this->token
->replace($this
->getSetting('alt_string'), $token_data, $replace_options)) {
$attributes['alt'] = $alt;
}
}
else {
$attributes['alt'] = $default_alt;
}
}
if ($this
->getSetting('enable_title')) {
if ($this
->getSetting('title_string')) {
if ($title = $this->token
->replace($this
->getSetting('title_string'), $token_data, $replace_options)) {
$attributes['title'] = $title;
}
}
else {
$attributes['title'] = $default_alt;
}
}
$uri = $item->entity
->getFileUri();
$svg_data = NULL;
if ($this
->getSetting('inline')) {
$svg_file = file_exists($uri) ? file_get_contents($uri) : NULL;
// Sanitize inline SVG if sanitizing library is installed.
if ($svg_file && $this
->isSanitizerInstalled() && $this
->getSetting('sanitize')) {
$sanitizer = new Sanitizer();
$svg_file = $sanitizer
->sanitize($svg_file);
}
if ($svg_file) {
$dom = new \DOMDocument();
libxml_use_internal_errors(TRUE);
$dom
->loadXML($svg_file);
if ($this
->getSetting('apply_dimensions') && isset($dom->documentElement)) {
$dom->documentElement
->setAttribute('height', $attributes['height']);
$dom->documentElement
->setAttribute('width', $attributes['width']);
}
if ($this
->getSetting('enable_title') && isset($dom->documentElement)) {
$title = $dom
->createElement('title', $attributes['title']);
$title_id = $this->fieldName . '__title-' . $delta;
$title
->setAttribute('id', $title_id);
$dom->documentElement
->insertBefore($title, $dom->documentElement->firstChild);
$dom->documentElement
->setAttribute('aria-labelledby', $title_id);
}
$svg_data = $dom
->saveXML($dom->documentElement);
}
}
$elements[$delta] = [
'#theme' => 'svg_formatter',
'#inline' => $this
->getSetting('inline') ? TRUE : FALSE,
'#attributes' => $attributes,
'#uri' => $this
->getSetting('inline') ? NULL : $uri,
'#svg_data' => $this
->getSetting('inline') ? $svg_data : NULL,
];
}
}
return $elements;
}