You are here

public function SmartDateTrait::viewElements in Smart Date 8.2

Same name and namespace in other branches
  1. 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
  2. 3.x 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) {
  $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 (!empty($item->value) && !empty($item->end_value)) {
      $timezone = $item->timezone ? $item->timezone : $timezone_override;
      $elements[$delta] = static::formatSmartDate($item->value, $item->end_value, $settings, $timezone);
      $elements[$delta]['#value'] = $item->value;
      $elements[$delta]['#end_value'] = $item->end_value;

      // 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($item->value, '', $settings['date_format'], $timezone);
        $user_date = \Drupal::service('date.formatter')
          ->format($item->value, '', $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($item->value, $item->end_value, $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;
}