Commit 983ff82f authored by git's avatar git Committed by johnv

Issue #2720335 by echo15, johnv: Support openingHours microdata from schema.org (D8)

parent 6e435a3d
......@@ -25,6 +25,7 @@ function office_hours_theme() {
$theme['office_hours_table'] = [
'variables' => [
'table' => NULL,
'schema' => NULL,
],
];
......@@ -54,6 +55,21 @@ function template_preprocess_office_hours(&$variables) {
// Minimum width for day labels. Adjusted when adding new labels.
$label_length = 3;
$items = [];
// Schema part.
// @todo: move to ..._preprocess_office_hours_schema, or use RDF module's twig.
if (isset($office_hours['schema'])) {
$schema_items = [];
foreach ($office_hours['schema'] as $schema) {
$schema_items[] = [
'label' => $schema['label'],
'slots' => ['#type' => 'markup', '#markup' => $schema['formatted_slots']],
];
}
$variables['schema'] = $schema_items;
unset($office_hours['schema']);
}
foreach ($office_hours as $info) {
$label = $info['label'];
$label_length = max($label_length, \Drupal\Component\Utility\Unicode::strlen($label));
......
......@@ -36,6 +36,9 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
'open_text' => 'Currently open!',
'closed_text' => 'Currently closed',
],
'schema' => [
'enabled' => FALSE,
],
'timezone_field' => '',
'office_hours_first_day' => '',
] + parent::defaultSettings();
......@@ -221,6 +224,20 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
'#description' => $this->t('Format of message displayed when currently closed.'),
];
$element['schema'] = [
'#title' => $this->t('Schema.org openingHours support'),
'#type' => 'details',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$element['schema']['enabled'] = [
'#title' => $this->t('Enable Schema.org openingHours support'),
'#type' => 'checkbox',
'#default_value' => $settings['schema']['enabled'],
'#description' => $this->t('Enable meta tags with property for Schema.org openingHours.'),
'#required' => FALSE,
];
/*
if ($timezone_fields) {
$element['timezone_field'] = [
......@@ -251,6 +268,44 @@ abstract class OfficeHoursFormatterBase extends FormatterBase {
return $summary;
}
/**
* Add an 'openingHours' formatter from https://schema.org/openingHours.
*
* @param FieldItemListInterface $items
* @param $langcode
* @param array $elements
*
* @return array
*/
protected function addSchemaFormatter(FieldItemListInterface $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 = array();
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),
],
];
return $elements;
}
/**
* Add a 'status' formatter before or after the hours, if necessary.
*
......
......@@ -63,6 +63,13 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase {
];
}
// @todo #2720335 : try to get the meta data into the <tr>.
// foreach ($table_rows as $delta => &$row) {
// $row['#metadata']['itemprop'] = "openingHours";
// $row['#metadata']['property'] = "openingHours";
// $row['#metadata']['content'] = "todo";
// }
$table = [
'#theme' => 'table',
'#attributes' => [
......@@ -87,6 +94,9 @@ class OfficeHoursFormatterTable extends OfficeHoursFormatterBase {
];
// Build the Schema part from https://schema.org/openingHours.
$elements[0] = $this->addSchemaFormatter($items, $langcode, $elements[0]);
// Build the Status part. May reorder elements.
$elements = $this->addStatusFormatter($items, $langcode, $elements);
......
<div class="office-hours">
{{table}}
{{ table }}
{% if schema %}
<div class="office-hours-schema">
{{ schema }}
</div>
{% endif %}
</div>
......@@ -7,4 +7,9 @@
<br />
</div>
{% endfor %}
{% if schema %}
{% for meta in schema %}
<meta property="openingHours" content="{{ meta.label }}{{ meta.slots }}">
{% endfor %}
{% endif %}
</div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment