OfficeHoursFormatterDefault.php in Office Hours 8
File
src/Plugin/Field/FieldFormatter/OfficeHoursFormatterDefault.php
View source
<?php
namespace Drupal\office_hours\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
class OfficeHoursFormatterDefault extends OfficeHoursFormatterBase {
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = '(When using multiple slots per day, better use the table formatter.)';
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
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',
],
],
'#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;
}
}