public function SmartDateTrait::viewElements in Smart Date 3.4.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.2.x src/SmartDateTrait.php \Drupal\smart_date\SmartDateTrait::viewElements()
- 3.3.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 17
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');
// Look for the Date Augmenter plugin manager service.
$augmenters = [];
if (!empty(\Drupal::hasService('plugin.manager.dateaugmenter'))) {
$dateAugmenterManager = \Drupal::service('plugin.manager.dateaugmenter');
// TODO: Support custom entities.
$config = $this
->getThirdPartySettings('date_augmenter');
$augmenters = $dateAugmenterManager
->getActivePlugins($config);
$entity = $items
->getEntity();
}
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();
}
elseif ($field_type == 'timestamp' || $field_type == 'published_at') {
if (empty($item->value)) {
continue;
}
$start_ts = $end_ts = $item->value;
}
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, $add_classes);
}
// 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 ($augmenters) {
foreach ($augmenters as $augmenter_id => $augmenter) {
// Use the enabled plugin to manipulate the output.
$augmenter
->augmentOutput($elements[$delta], DrupalDateTime::createFromTimestamp($start_ts), DrupalDateTime::createFromTimestamp($end_ts), [
'timezone' => $timezone,
'allday' => static::isAllDay($start_ts, $end_ts, $timezone),
'entity' => $entity,
'settings' => $config['settings'][$augmenter_id],
'delta' => $delta,
'formatter' => $this,
'field_name' => $this->fieldDefinition
->getName(),
]);
}
}
}
// 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;
}