public function LibraryItemFieldFormatter::viewElements in Library 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/ LibraryItemFieldFormatter.php, line 30
Class
- LibraryItemFieldFormatter
- Plugin implementation of the 'library_item_field_formatter' formatter.
Namespace
Drupal\library\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) : array {
$elements = [
'#type' => 'table',
'#title' => $this
->t('Library items'),
'#header' => [
'Barcode',
'Library status',
'Notes',
'Actions',
],
];
$rows = [];
foreach ($items as $delta => $target) {
/** @var \Drupal\library\Entity\LibraryItem $item */
$item = \Drupal::entityTypeManager()
->getStorage('library_item')
->load($target
->getValue()['target_id']);
if ($item
->get('barcode')->value || $item
->get('in_circulation')->value) {
$rows[$delta]['barcode'] = nl2br(new HtmlEscapedText($item
->get('barcode')->value));
$rows[$delta]['library_status'] = $this
->checkAvailability($item
->get('in_circulation')->value, $item
->get('library_status')->value);
$rows[$delta]['notes'] = nl2br(new HtmlEscapedText($item
->get('notes')->value));
$actions = $this
->getActions($item
->get('in_circulation')->value, $item
->get('library_status')->value, $target
->getValue()['target_id']);
if ($actions) {
$actions = [
'#type' => 'operations',
'#links' => $actions,
];
$rows[$delta]['actions'] = \Drupal::service('renderer')
->render($actions);
}
else {
unset($elements['#header'][3]);
}
}
}
$elements['#rows'] = $rows;
return $elements;
}