You are here

public function SmartDateTrait::viewElements in Smart Date 3.x

Same name and namespace in other branches
  1. 8.2 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  2. 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  3. 3.0.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  4. 3.1.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  5. 3.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  6. 3.3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  7. 3.4.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
3 methods override SmartDateTrait::viewElements()
SmartDateDurationFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDateDurationFormatter.php
SmartDatePlainFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php
Builds a renderable array for a field value.
SmartDateRecurrenceFormatter::viewElements in modules/smart_date_recur/src/Plugin/Field/FieldFormatter/SmartDateRecurrenceFormatter.php

File

src/SmartDateTrait.php, line 16

Class

SmartDateTrait
Provides friendly methods for smart date range.

Namespace

Drupal\smart_date

Code

public function viewElements(FieldItemListInterface $items, $langcode) {
  $field_type = $this->fieldDefinition
    ->getType();
  $elements = [];

  // TODO: intelligent switching between retrieval methods.
  // Look for a defined format and use it if specified.
  $format_label = $this
    ->getSetting('format');
  if ($format_label) {
    $entity_storage_manager = \Drupal::entityTypeManager()
      ->getStorage('smart_date_format');
    $format = $entity_storage_manager
      ->load($format_label);
    $settings = $format
      ->getOptions();
  }
  else {
    $settings = [
      'separator' => $this
        ->getSetting('separator'),
      'join' => $this
        ->getSetting('join'),
      'time_format' => $this
        ->getSetting('time_format'),
      'time_hour_format' => $this
        ->getSetting('time_hour_format'),
      'date_format' => $this
        ->getSetting('date_format'),
      'date_first' => $this
        ->getSetting('date_first'),
      'ampm_reduce' => $this
        ->getSetting('ampm_reduce'),
      'allday_label' => $this
        ->getSetting('allday_label'),
    ];
  }
  $timezone_override = $this
    ->getSetting('timezone_override') ?: NULL;
  foreach ($items as $delta => $item) {
    if ($field_type == 'smartdate') {
      if (empty($item->value) || empty($item->end_value)) {
        continue;
      }
      $start_ts = $item->value;
      $end_ts = $item->end_value;
    }
    elseif ($field_type == 'daterange') {
      if (empty($item->start_date) || empty($item->end_date)) {
        continue;
      }
      $start_ts = $item->start_date
        ->getTimestamp();
      $end_ts = $item->end_date
        ->getTimestamp();
    }
    elseif ($field_type == 'datetime') {
      if (empty($item->date)) {
        continue;
      }
      $start_ts = $end_ts = $item->date
        ->getTimestamp();
    }
    else {

      // Not sure how to handle anything else, so return an empty set.
      return $elements;
    }
    $timezone = $item->timezone ? $item->timezone : $timezone_override;
    $elements[$delta] = static::formatSmartDate($start_ts, $end_ts, $settings, $timezone);
    $elements[$delta]['#value'] = $start_ts;
    $elements[$delta]['#end_value'] = $end_ts;

    // Get the user/site timezone for comparison.
    $user = \Drupal::currentUser();
    $user_tz = $user
      ->getTimeZone();
    if ($timezone && $timezone != $user_tz) {

      // Uses a custom timezone, so append time in default timezone.
      $no_date_format = $settings;
      $default_date = \Drupal::service('date.formatter')
        ->format($start_ts, '', $settings['date_format'], $timezone);
      $user_date = \Drupal::service('date.formatter')
        ->format($start_ts, '', $settings['date_format'], $user_tz);

      // If the date is the same in both timezones, only display it once.
      if ($default_date == $user_date) {
        $no_date_format['date_format'] = '';
      }
      $site_time = static::formatSmartDate($start_ts, $end_ts, $no_date_format, $user_tz);
      $site_time['#prefix'] = ' (';
      $site_time['#suffix'] = ')';
      $elements[$delta]['site_time'] = $site_time;
    }
    if (!empty($item->_attributes)) {
      $elements[$delta]['#attributes'] += $item->_attributes;

      // Unset field item attributes since they have been included in the
      // formatter output and should not be rendered in the field template.
      unset($item->_attributes);
    }
  }

  // If specified, sort based on start, end times.
  if ($this
    ->getSetting('force_chronological')) {
    $elements = smart_date_array_orderby($elements, '#value', SORT_ASC, '#end_value', SORT_ASC);
  }
  return $elements;
}