public function OfficeHoursFormatterDefault::viewElements in Office Hours 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/ OfficeHoursFormatterDefault.php, line 32
Class
- OfficeHoursFormatterDefault
- Plugin implementation of the formatter.
Namespace
Drupal\office_hours\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
// If no data is filled for this entity, do not show the formatter.
// N.B. 'Show current day' may return nothing in getRows(), while other days are filled.
/** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */
if (!$items
->getValue()) {
return $elements;
}
$settings = $this
->getSettings();
$office_hours = $this
->getRows($items
->getValue(), $this
->getSettings(), $this
->getFieldSettings());
if ($office_hours) {
$elements[] = [
'#theme' => 'office_hours',
'#office_hours' => $office_hours,
'#item_separator' => $settings['separator']['days'],
'#slot_separator' => $settings['separator']['more_hours'],
'#attributes' => [
'class' => [
'office-hours',
],
],
// '#empty' => $this->t('This location has no opening hours.'),
'#attached' => [
'library' => [
'office_hours/office_hours_formatter',
],
],
'#cache' => [
'max-age' => $this
->getStatusTimeLeft($items, $langcode),
'tags' => [
'office_hours:field.default',
],
],
];
}
$elements = $this
->addStatusFormatter($items, $langcode, $elements);
return $elements;
}