You are here

function office_hours_tokens in Office Hours 8

Implements hook_tokens().

File

./office_hours.tokens.inc, line 15
Provides tokens for the office_hours module.

Code

function office_hours_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if (empty($data['field_property'])) {
    return $replacements;
  }
  foreach ($tokens as $token => $original) {
    $list = $data[$data['field_name']];
    if (!$list instanceof OfficeHoursItemList) {
      continue;
    }
    $parts = explode(':', $token);
    $field = $list
      ->get($parts[0]);
    $day = $field
      ->get($parts[1])
      ->getValue();
    switch ($parts[1]) {
      case 'day':
        $day_names = OfficeHoursDateHelper::weekDaysByFormat('long');
        $replacements[$original] = $day_names[$day];
        break;
      case 'starthours':
      case 'endhours':
        $replacements[$original] = OfficeHoursDateHelper::format($day, "H:i:s", FALSE);
        break;
    }
  }
  return $replacements;
}