public function SmartDateTrait::viewElements in Smart Date 3.2.x
Same name and namespace in other branches
- 8.2 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 8 src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.0.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.1.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.3.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.4.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
4 methods override SmartDateTrait::viewElements()
- SmartDateDailyRangeFormatter::viewElements in modules/
smart_date_recur/ src/ Plugin/ Field/ FieldFormatter/ SmartDateDailyRangeFormatter.php - 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_dateCode
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'),
'site_time_toggle' => $this
->getSetting('site_time_toggle'),
'allday_label' => $this
->getSetting('allday_label'),
];
}
$timezone_override = $this
->getSetting('timezone_override') ?: NULL;
$add_classes = $this
->getSetting('add_classes');
$time_wrapper = $this
->getSetting('time_wrapper');
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);
if ($add_classes) {
$this
->addRangeClasses($elements[$delta]);
}
if ($time_wrapper) {
$this
->addTimeWrapper($elements[$delta], $start_ts, $end_ts, $timezone);
}
// Attach the timestamps in case they're needed for later processing.
$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 (!static::isAllDay($start_ts, $end_ts, $timezone) && $settings['site_time_toggle'] && $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);
// Only process further if a value is returned.
if ($site_time) {
$event_time = static::formatSmartDate($start_ts, $end_ts, $no_date_format, $timezone);
// Only append if displayed time will be different.
if ($site_time != $event_time) {
$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;
}