You are here

public function OfficeHoursFormatterStatus::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/OfficeHoursFormatterStatus.php, line 47

Class

OfficeHoursFormatterStatus
Plugin implementation of the formatter.

Namespace

Drupal\office_hours\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];
  $settings = $this
    ->getSettings();

  // Alter the default settings, to calculate the cache correctly.
  // The status formatter has no UI for this setting.
  $this
    ->setSetting('show_closed', 'next');

  /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */
  $elements += [
    '#theme' => 'office_hours_status',
    '#is_open' => $items
      ->isOpen(),
    '#open_text' => (string) $this
      ->t($settings['current_status']['open_text']),
    '#closed_text' => (string) $this
      ->t($settings['current_status']['closed_text']),
    '#position' => $this->settings['current_status']['position'],
    '#cache' => [
      'max-age' => $this
        ->getStatusTimeLeft($items, $langcode),
      'tags' => [
        'office_hours:field.status',
      ],
    ],
  ];
  return $elements;
}