You are here

protected function SmartDateTrait::addTimeWrapper in Smart Date 3.4.x

Same name and namespace in other branches
  1. 3.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::addTimeWrapper()
  2. 3.3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::addTimeWrapper()

Add spans provides classes to allow the dates and times to be styled.

Parameters

array $instance: The render array of the formatted date range.

object $start_ts: A timestamp.

object $end_ts: A timestamp.

string|null $timezone: An optional timezone override.

bool $add_classes: Whether or not the field is also adding class wrappers.

4 calls to SmartDateTrait::addTimeWrapper()
SmartDateDailyRangeFormatter::viewElements in modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateDailyRangeFormatter.php
SmartDateDurationFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php
SmartDateRecurrenceFormatter::viewElements in modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateRecurrenceFormatter.php
SmartDateTrait::viewElements in src/SmartDateTrait.php

File

src/SmartDateTrait.php, line 221

Class

SmartDateTrait
Provides friendly methods for smart date range.

Namespace

Drupal\smart_date

Code

protected function addTimeWrapper(array &$instance, $start_ts, $end_ts, $timezone = NULL, $add_classes = FALSE) {
  $times = [
    'start' => $start_ts,
    'end' => $end_ts,
  ];

  // Only add the time wrappers inside if there is an incomplete range part.
  if ((isset($instance['start']['date']) xor isset($instance['start']['time'])) || (isset($instance['end']['date']) xor isset($instance['end']['time']))) {
    $inner_wrappers = TRUE;
  }
  else {
    $inner_wrappers = FALSE;
  }
  foreach ([
    'start',
    'end',
  ] as $part) {
    if (isset($instance[$part])) {
      if ($this
        ->isAllDay($start_ts, $end_ts, $timezone)) {
        $format = 'Y-m-d';
      }
      else {
        $format = 'c';
      }
      $datetime = \Drupal::service('date.formatter')
        ->format($times[$part], 'custom', $format);
      if ($add_classes && $inner_wrappers) {

        // If wrappers for classes have also been added, we need separate
        // time elements for the date and time, if set.
        foreach ([
          'date',
          'time',
        ] as $subpart) {
          if (isset($instance[$part][$subpart]) && $instance[$part][$subpart]) {
            $current_contents = $instance[$part][$subpart];
            unset($current_contents['#prefix']);
            unset($current_contents['#suffix']);
            $prefix = isset($instance[$part][$subpart]['#prefix']) ? $instance[$part][$subpart]['#prefix'] : NULL;
            $suffix = isset($instance[$part][$subpart]['#suffix']) ? $instance[$part][$subpart]['#suffix'] : NULL;
            $instance[$part][$subpart] = [
              '#theme' => 'time',
              '#attributes' => [
                'datetime' => $datetime,
              ],
              '#text' => $current_contents,
              '#prefix' => $prefix,
              '#suffix' => $suffix,
            ];
          }
        }
      }
      else {
        $current_contents = $instance[$part];
        $instance[$part] = [
          '#theme' => 'time',
          '#attributes' => [
            'datetime' => $datetime,
          ],
          '#text' => $current_contents,
        ];
      }
    }
  }
}