public function Barcode::viewElements in Barcodes 8
Same name and namespace in other branches
- 2.0.x src/Plugin/Field/FieldFormatter/Barcode.php \Drupal\barcodes\Plugin\Field\FieldFormatter\Barcode::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 FormatterInterface::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ Barcode.php, line 146
Class
- Barcode
- Plugin implementation of the 'barcode' formatter.
Namespace
Drupal\barcodes\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$token_service = \Drupal::token();
$generator = new BarcodeGenerator();
foreach ($items as $delta => $item) {
$suffix = str_replace('+', 'plus', strtolower($this
->getSetting('type')));
$tokens = [];
if ($entity = $items
->getEntity()) {
$tokens[$entity
->getEntityTypeId()] = $entity;
}
$value = $token_service
->replace($this
->viewValue($item), $tokens);
$elements[$delta] = [
'#theme' => 'barcode__' . $suffix,
'#attached' => [
'library' => [
'barcodes/' . $suffix,
],
],
'#type' => $this
->getSetting('type'),
'#value' => $value,
'#width' => $this
->getSetting('width'),
'#height' => $this
->getSetting('height'),
'#color' => $this
->getSetting('color'),
'#padding_top' => $this
->getSetting('padding_top'),
'#padding_right' => $this
->getSetting('padding_right'),
'#padding_bottom' => $this
->getSetting('padding_bottom'),
'#padding_left' => $this
->getSetting('padding_left'),
'#show_value' => $this
->getSetting('show_value'),
];
try {
$barcode = $generator
->getBarcodeObj($this
->getSetting('type'), $value, $this
->getSetting('width'), $this
->getSetting('height'), $this
->getSetting('color'), [
$this
->getSetting('padding-top'),
$this
->getSetting('padding-right'),
$this
->getSetting('padding-bottom'),
$this
->getSetting('padding-left'),
]);
$elements[$delta]['#format'] = $this
->getSetting('format');
$elements[$delta]['#svg'] = $barcode
->getSvgCode();
$elements[$delta]['#png'] = "<img alt=\"Embedded Image\" src=\"data:image/png;base64," . base64_encode($barcode
->getPngData()) . "\" />";
$elements[$delta]['#htmldiv'] = $barcode
->getHtmlDiv();
$elements[$delta]['#unicode'] = "<pre style=\"font-family:monospace;line-height:0.61em;font-size:6px;\">" . $barcode
->getGrid(json_decode('"\\u00A0"'), json_decode('"\\u2584"')) . "</pre>";
$elements[$delta]['#binary'] = "<pre style=\"font-family:monospace;\">" . $barcode
->getGrid() . "</pre>";
$elements[$delta]['#barcode'] = $elements[$delta]['#' . strtolower($this
->getSetting('format'))];
$elements[$delta]['#extended_value'] = $barcode
->getExtendedCode();
} catch (\Exception $e) {
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */
$logger = \Drupal::service('logger.factory')
->get('barcodes');
$logger
->error('Error: @error, given: @value', [
'@error' => $e
->getMessage(),
'@value' => $this
->viewValue($item),
]);
}
}
return $elements;
}