public function DateTimePlainFormatter::viewElements

Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

Overrides FormatterInterface::viewElements

File

drupal/core/modules/datetime/lib/Drupal/datetime/Plugin/field/formatter/DatetimePlainFormatter.php, line 33
Contains \Drupal\datetime\Plugin\field\formatter\DateTimePlainFormatter.

Class

DateTimePlainFormatter
Plugin implementation of the 'datetime_plain' formatter.

Namespace

Drupal\datetime\Plugin\field\formatter

Code

public function viewElements(EntityInterface $entity, $langcode, array $items) {
  $elements = array();
  foreach ($items as $delta => $item) {
    $output = '';
    if (!empty($item['date'])) {

      // The date was created and verified during field_load(), so it is safe
      // to use without further inspection.
      $date = $item['date'];
      $date
        ->setTimeZone(timezone_open(drupal_get_user_timezone()));
      $format = DATETIME_DATETIME_STORAGE_FORMAT;
      if ($this->field['settings']['datetime_type'] == 'date') {

        // A date without time will pick up the current time, use the default.
        datetime_date_default_time($date);
        $format = DATETIME_DATE_STORAGE_FORMAT;
      }
      $output = $date
        ->format($format);
    }
    $elements[$delta] = array(
      '#markup' => $output,
    );
  }
  return $elements;
}