You are here

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

Class

OfficeHoursFormatterSchema
Plugin implementation of the formatter, from https://schema.org/openingHours.

Namespace

Drupal\office_hours\Plugin\Field\FieldFormatter

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $elements = [];

  // Get some settings from field. Do not overwrite defaults.
  $settings = $this
    ->defaultSettings();
  unset($settings['compress']);
  unset($settings['grouped']);
  unset($settings['show_closed']);
  $settings += $this
    ->getSettings();

  /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items */
  $office_hours = $this
    ->getRows($items
    ->getValue(), $this
    ->getSettings(), $this
    ->getFieldSettings());
  $elements[] = [
    '#theme' => 'office_hours_schema',
    '#office_hours' => $office_hours,
    '#item_separator' => $settings['separator']['days'],
    '#slot_separator' => $settings['separator']['more_hours'],
    '#attributes' => [
      'class' => [
        'office-hours',
      ],
    ],
    '#cache' => [
      'max-age' => $this
        ->getStatusTimeLeft($items, $langcode),
      'tags' => [
        'office_hours:field.schema',
      ],
    ],
  ];
  return $elements;
}