Commit b26a68bc authored by John Voskuilen's avatar John Voskuilen
Browse files

Issue #3208978 by Dmitriy.trt, splash112, gumanist, johnv: Add...

Issue #3208978 by Dmitriy.trt, splash112, gumanist, johnv: Add schema.org/OpeningHoursSpecification microdata via Metatag+Token module
parent 61319414
Loading
Loading
Loading
Loading
+29 −13
Original line number Diff line number Diff line
@@ -26,21 +26,37 @@ function office_hours_tokens($type, $tokens, array $data, array $options, Bubble
      continue;
    }

    // @todo Update patch once multiple values support added to Token module:
    // @see https://www.drupal.org/project/token/issues/3115486
    $parts = explode(':', $token);
    $field = $list->get($parts[0]);

    $day = $field->get($parts[1])->getValue();
    switch ($parts[1]) {
    $resulting_items = [];
    if (is_numeric($parts[0])) {
      $list = [$list->get($parts[0])];
      $property = $parts[1];
    }
    else {
      $property = $parts[0];
    }
    foreach ($list as $item) {
      /** @var \Drupal\office_hours\Plugin\Field\FieldType\OfficeHoursItem $item */
      switch ($property) {
        case 'day':
          $day_names = OfficeHoursDateHelper::weekDaysByFormat('long');
        $replacements[$original] = $day_names[$day];
          $resulting_items[] = $day_names[$item->{$property}];
          break;

        case 'day-untranslated':
          $day_names = OfficeHoursDateHelper::weekDaysByFormat('long_untranslated');
          $resulting_items[] = $day_names[$item->day];
          break;

        case 'starthours':
        case 'endhours':
        $replacements[$original] = OfficeHoursDateHelper::format($day, "H:i:s", FALSE);
          $resulting_items[] = OfficeHoursDateHelper::format($item->{$property}, "H:i:s", FALSE);
          break;
      }
    }
    $replacements[$original] = implode(',', $resulting_items);
  }
  return $replacements;
}
+10 −5
Original line number Diff line number Diff line
@@ -201,7 +201,6 @@ class OfficeHoursDateHelper extends DateHelper {
    $none = ['' => ''];
    $hours = !$required ? $none + $hours : $hours;
    // End modified copy from date_hours().

    return $hours;
  }

@@ -231,6 +230,10 @@ class OfficeHoursDateHelper extends DateHelper {
        $days = self::weekDays(TRUE);
        break;

      case 'long_untranslated':
        $days = self::weekDaysUntranslated();
        break;

      case 'two_letter':
        // @todo Avoid translation from English to XX, in case of 'Microdata'.
        $days = self::weekDaysAbbr2(TRUE);
@@ -281,7 +284,7 @@ class OfficeHoursDateHelper extends DateHelper {
  /**
   * Creates a date object from an input format.
   *
   * Wrapper function to centralize all Date/Timefunctions into DateHelper class.
   * Wrapper function to centralize all Date/Time functions into this class.
   *
   * @param string $format
   *   PHP date() type format for parsing the input. This is recommended
@@ -289,7 +292,9 @@ class OfficeHoursDateHelper extends DateHelper {
   *   any other specialized input with a known format. If provided the
   *   date will be created using the createFromFormat() method.
   * @param mixed $time
   *   A time.
   * @param mixed $timezone
   *   A timezone.
   * @param array $settings
   *   - validate_format: (optional) Boolean choice to validate the
   *     created date using the input format. The format used in
@@ -312,8 +317,9 @@ class OfficeHoursDateHelper extends DateHelper {
  /**
   * Returns whether the day number is a weekday or a date (Exception day).
   *
   * @param $day_number
   * @param mixed $day_number
   *   Day (using date_api as key (0=Sun, 6=Sat)) or exception day date.
   *
   * @return bool
   *   False if the day_number is a (sorted) weekday (-7 until +7).
   */
@@ -321,5 +327,4 @@ class OfficeHoursDateHelper extends DateHelper {
    return !(((int) $day_number) < 8);
  }


}