You are here

public function SmartDateTrait::viewElements in Smart Date 8

Same name and namespace in other branches
  1. 8.2 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()
1 method overrides SmartDateTrait::viewElements()
SmartDatePlainFormatter::viewElements in src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php
Builds a renderable array for a field value.

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: intellident 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'),
    ];
  }
  foreach ($items as $delta => $item) {
    if (!empty($item->value) && !empty($item->end_value)) {
      $elements[$delta] = static::formatSmartDate($item->value, $item->end_value, $settings);
      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);
      }
    }
  }
  return $elements;
}