You are here

protected function OfficeHoursFormatterBase::addSchemaFormatter in Office Hours 8

Add an 'openingHours' formatter from https://schema.org/openingHours.

Parameters

\Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemListInterface $items:

$langcode:

array $elements:

Return value

array

1 call to OfficeHoursFormatterBase::addSchemaFormatter()
OfficeHoursFormatterTable::viewElements in src/Plugin/Field/FieldFormatter/OfficeHoursFormatterTable.php
Builds a renderable array for a field value.

File

src/Plugin/Field/FieldFormatter/OfficeHoursFormatterBase.php, line 284

Class

OfficeHoursFormatterBase
Abstract plugin implementation of the formatter.

Namespace

Drupal\office_hours\Plugin\Field\FieldFormatter

Code

protected function addSchemaFormatter(OfficeHoursItemListInterface $items, $langcode, array $elements) {
  $formatter = new OfficeHoursFormatterSchema($this->pluginId, $this->pluginDefinition, $this->fieldDefinition, $this->settings, $this->viewMode, $this->label, $this->thirdPartySettings);
  $new_element = $formatter
    ->viewElements($items, $langcode);
  $schema_items = [];
  foreach ($new_element[0]['#office_hours'] as $schema) {
    $schema_items[] = [
      'label' => $schema['label'],
      'formatted_slots' => $schema['formatted_slots'],
    ];
  }
  $elements['#schema'] = [
    '#theme' => 'office_hours',
    '#office_hours' => [
      'schema' => $schema_items,
    ],
    '#cache' => [
      'max-age' => $this
        ->getStatusTimeLeft($items, $langcode),
      'tags' => [
        'office_hours:field.default',
      ],
    ],
  ];
  return $elements;
}