You are here

office_hours.tokens.inc in Office Hours 8

Provides tokens for the office_hours module.

File

office_hours.tokens.inc
View source
<?php

/**
 * @file
 * Provides tokens for the office_hours module.
 */
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\office_hours\OfficeHoursDateHelper;
use Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItemList;

/**
 * Implements hook_tokens().
 */
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;
}

Functions

Namesort descending Description
office_hours_tokens Implements hook_tokens().