public function OfficeHoursFormatterBase::getStatusTimeLeft in Office Hours 8
5 calls to OfficeHoursFormatterBase::getStatusTimeLeft()
- OfficeHoursFormatterBase::addSchemaFormatter in src/
Plugin/ Field/ FieldFormatter/ OfficeHoursFormatterBase.php - Add an 'openingHours' formatter from https://schema.org/openingHours.
- OfficeHoursFormatterDefault::viewElements in src/
Plugin/ Field/ FieldFormatter/ OfficeHoursFormatterDefault.php - Builds a renderable array for a field value.
- OfficeHoursFormatterSchema::viewElements in src/
Plugin/ Field/ FieldFormatter/ OfficeHoursFormatterSchema.php - Builds a renderable array for a field value.
- OfficeHoursFormatterStatus::viewElements in src/
Plugin/ Field/ FieldFormatter/ OfficeHoursFormatterStatus.php - Builds a renderable array for a field value.
- 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 352
Class
- OfficeHoursFormatterBase
- Abstract plugin implementation of the formatter.
Namespace
Drupal\office_hours\Plugin\Field\FieldFormatterCode
public function getStatusTimeLeft(OfficeHoursItemListInterface $items, $langcode) {
// @see https://www.drupal.org/docs/8/api/cache-api/cache-max-age
// If there are no open days, cache forever.
if (empty($items
->getValue())) {
return Cache::PERMANENT;
}
$date = new DrupalDateTime('now');
$today = $date
->format('w');
$now = $date
->format('Hi');
$seconds = $date
->format('s');
$next_time = '0000';
$add_days = 0;
// Get some settings from field. Do not overwrite defaults.
$settings = $this
->getSettings();
// Return the filtered days/slots/items/rows.
switch ($settings['show_closed']) {
case 'all':
case 'open':
case 'none':
// These caches never expire, since they are always correct.
return Cache::PERMANENT;
case 'current':
// Cache expires at midnight.
$next_time = '0000';
$add_days = 1;
break;
case 'next':
// Get the first (and only) day of the list.
// Make sure we only receive 1 day, only to calculate the cache.
$office_hours = $this
->getRows($items
->getValue(), $this
->getSettings(), $this
->getFieldSettings());
$next = array_shift($office_hours);
// Get the difference in hours/minutes between 'now' and next open/closing time.
$first_time = NULL;
foreach ($next['slots'] as $slot) {
$start = $slot['start'];
$end = $slot['end'];
if ($next['startday'] != $today) {
// We will open tomorrow or later.
$next_time = $start;
$add_days = ($next['startday'] - $today + 7) % 7;
break;
}
elseif ($start > $now) {
// We will open later today.
$next_time = $start;
$add_days = 0;
break;
}
elseif ($start > $end || $start == $end || $start < $end && $end > $now) {
$next_time = $end;
$add_days = $start < $end ? 0 : 1;
// Add 1 day if open until after midnight.
break;
}
else {
// We were open today. Take the first slot of the day.
if (!isset($first_time_slot_found)) {
$first_time_slot_found = TRUE;
$next_time = $start;
$add_days = 7;
}
continue;
// A new slot might come along.
}
}
break;
default:
// We should have covered all options above.
return Cache::PERMANENT;
}
// Set to 0 to avoid php error if time field is not set.
$next_time = is_numeric($next_time) ? $next_time : '0000';
// Calculate the remaining cache time.
$time_left = (int) $add_days * 24 * 3600;
$time_left += (int) (substr($next_time, 0, 2) - substr($now, 0, 2)) * 3600;
$time_left += (int) (substr($next_time, 2, 2) - substr($now, 2, 2)) * 60;
$time_left -= $seconds;
// Correct for the current minute.
return $time_left;
}