You are here

protected function OfficeHoursFormatterTrait::formatLabels in Office Hours 8

Formatter: format the day name.

Parameters

array $office_hours: Office hours array.

array $settings: User settings array.

Return value

array Reformatted office hours array.

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

File

src/OfficeHoursFormatterTrait.php, line 287

Class

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

Namespace

Drupal\office_hours

Code

protected function formatLabels(array $office_hours, array $settings) {
  $day_format = $settings['day_format'];
  $day_names = OfficeHoursDateHelper::weekDaysByFormat($settings['day_format']);
  $group_separator = $settings['separator']['grouped_days'];
  $days_suffix = $settings['separator']['day_hours'];
  foreach ($office_hours as $key => &$info) {

    // @todo Expose the date format as a formatter option.
    // @todo Use OfficeHoursDateHelper::getTimeFormat for dates.

    /** @var \Drupal\Core\StringTranslation\TranslatableMarkup $label */
    $label = isset($day_names[$info['startday']]) ? $day_names[$info['startday']] : \Drupal::service('date.formatter')
      ->format($info['startday'], 'html_date');

    // For Exception days.
    if ($day_format == 'none') {
      $info['label'] = $label;
      continue;
    }
    if (isset($info['endday'])) {

      // Adding 'Group days' support for dates seems not feasible.
      $label .= $group_separator . $day_names[$info['endday']];
    }
    $info['label'] = $label ? $label . $days_suffix : '';
  }
  return $office_hours;
}