public function Base64ImageFormatter::viewElements in Entity Print 8.2
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/ Base64ImageFormatter.php, line 49
Class
- Base64ImageFormatter
- Plugin implementation of the 'entity_print_base64_image_formatter' formatter.
Namespace
Drupal\entity_print\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
/** @var \Drupal\file\Entity\File $file */
foreach ($items as $delta => $item) {
$file = $item->entity;
$uri = $file
->getFileUri();
// If we have a image style, use that instead.
if ($this
->getSetting('image_style')) {
$image_style = ImageStyle::load($this
->getSetting('image_style'));
$uri = $this
->getImageStyleUri($image_style, $file) ?: $uri;
}
$base_64_image = base64_encode(file_get_contents($uri));
$filemime = $file
->getMimeType();
$element[$delta] = [
'#theme' => 'image',
'#uri' => "data:{$filemime};charset=utf-8;base64,{$base_64_image}",
];
}
return $element;
}