You are here

protected function OfficeHoursFormatterTrait::groupDays in Office Hours 8

Formatter: group days with same slots into 1 line.

Parameters

array $office_hours: Office hours array.

Return value

array Reformatted office hours array.

1 call to OfficeHoursFormatterTrait::groupDays()
OfficeHoursFormatterTrait::getRows in src/OfficeHoursFormatterTrait.php
Returns the items of a field.

File

src/OfficeHoursFormatterTrait.php, line 193

Class

OfficeHoursFormatterTrait
Factors out OfficeHoursItemList->getItems()->getRows().

Namespace

Drupal\office_hours

Code

protected function groupDays(array $office_hours) {
  $previous_day = NULL;

  // Keys -7 to +7 are for sorted weekdays.
  $previous_key = -100;
  foreach ($office_hours as $key => &$day) {

    // N.B. for 0=Sundays, we need to (int) the indices.
    // @todo Enable groupDays() for Exception days.
    if ($day['slots'] == $previous_day['slots'] && $key < 8) {
      $day['endday'] = $day['startday'];
      $day['startday'] = $previous_day['startday'];
      $day['current'] = $day['current'] || $previous_day['current'];
      $day['next'] = $day['next'] || $previous_day['next'];
      unset($office_hours[(int) $previous_key]);
    }
    $previous_key = (int) $key;
    $previous_day = $day;
  }
  return $office_hours;
}