public function SmartDatePlainFormatter::viewElements in Smart Date 3.0.x
Same name and namespace in other branches
- 8.2 src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 8 src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 3.x src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 3.1.x src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 3.2.x src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 3.3.x src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
- 3.4.x src/Plugin/Field/FieldFormatter/SmartDatePlainFormatter.php \Drupal\smart_date\Plugin\Field\FieldFormatter\SmartDatePlainFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides SmartDateTrait::viewElements
File
- src/
Plugin/ Field/ FieldFormatter/ SmartDatePlainFormatter.php, line 41
Class
- SmartDatePlainFormatter
- Plugin implementation of the 'Plain' formatter for 'smartdate' fields.
Namespace
Drupal\smart_date\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$separator = $this
->getSetting('separator');
foreach ($items as $delta => $item) {
$item->start_time = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
$item->end_time = isset($items[$delta]->end_value) ? DrupalDateTime::createFromTimestamp($items[$delta]->end_value) : '';
if (!empty($item->start_time) && !empty($item->end_time)) {
/** @var \Drupal\Core\Datetime\DrupalDateTime $start_time */
$start_time = $item->start_time;
/** @var \Drupal\Core\Datetime\DrupalDateTime $end_time */
$end_time = $item->end_time;
if ($start_time
->getTimestamp() !== $end_time
->getTimestamp()) {
$elements[$delta] = [
'start_time' => $this
->buildDate($start_time),
'separator' => [
'#plain_text' => ' ' . $separator . ' ',
],
'end_time' => $this
->buildDate($end_time),
];
}
else {
$elements[$delta] = $this
->buildDate($start_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 render in the field template.
unset($item->_attributes);
}
}
}
}
return $elements;
}