You are here

protected function OfficeHoursFormatterTrait::formatSlots in Office Hours 8

Formatter: format the office hours list.

Parameters

array $office_hours: Office hours array.

array $settings: User settings array.

array $field_settings: User field settings array.

Return value

array Reformatted office hours array.

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

File

src/OfficeHoursFormatterTrait.php, line 328

Class

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

Namespace

Drupal\office_hours

Code

protected function formatSlots(array $office_hours, array $settings, array $field_settings) {
  $time_format = OfficeHoursDateHelper::getTimeFormat($settings['time_format']);
  $time_separator = $settings['separator']['hours_hours'];
  $slot_separator = $settings['separator']['more_hours'];
  foreach ($office_hours as &$day_data) {
    $day_data['formatted_slots'] = [];
    $day_data['comments'] = [];
    foreach ($day_data['slots'] as $key => &$slot_data) {
      $formatted_slot = OfficeHoursDateHelper::formatTimeSlot($slot_data['start'], $slot_data['end'], $time_format, $time_separator);

      // Store the formatted slot in the slot itself.
      $slot_data['formatted_slot'] = $formatted_slot;

      // Store the arrays of formatted slots & comments in the day.
      $day_data['formatted_slots'][] = $formatted_slot;

      // Always add comment to keep aligned with time slot.
      $day_data['comments'][] = $slot_data['comment'];
    }
    $day_data['formatted_slots'] = empty($day_data['formatted_slots']) ? $day_data['closed'] : implode($slot_separator, $day_data['formatted_slots']);

    // Escape and Translate the comments.
    $day_data['comments'] = array_map('Drupal\\Component\\Utility\\Html::escape', $day_data['comments']);
    if ($field_settings['comment'] == 2) {
      $day_data['comments'] = array_map('t', $day_data['comments']);
    }
    $day_data['comments'] = $field_settings['comment'] ? implode($slot_separator, $day_data['comments']) : '';
  }
  return $office_hours;
}