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\FieldFormatter
Code
public function getStatusTimeLeft(OfficeHoursItemListInterface $items, $langcode) {
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;
$settings = $this
->getSettings();
switch ($settings['show_closed']) {
case 'all':
case 'open':
case 'none':
return Cache::PERMANENT;
case 'current':
$next_time = '0000';
$add_days = 1;
break;
case 'next':
$office_hours = $this
->getRows($items
->getValue(), $this
->getSettings(), $this
->getFieldSettings());
$next = array_shift($office_hours);
$first_time = NULL;
foreach ($next['slots'] as $slot) {
$start = $slot['start'];
$end = $slot['end'];
if ($next['startday'] != $today) {
$next_time = $start;
$add_days = ($next['startday'] - $today + 7) % 7;
break;
}
elseif ($start > $now) {
$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;
break;
}
else {
if (!isset($first_time_slot_found)) {
$first_time_slot_found = TRUE;
$next_time = $start;
$add_days = 7;
}
continue;
}
}
break;
default:
return Cache::PERMANENT;
}
$next_time = is_numeric($next_time) ? $next_time : '0000';
$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;
return $time_left;
}