Version 1
[yaffs-website] / web / core / modules / datetime / src / Plugin / Field / FieldFormatter / DateTimeDefaultFormatter.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\Core\Form\FormStateInterface;
8
9 /**
10  * Plugin implementation of the 'Default' formatter for 'datetime' fields.
11  *
12  * @FieldFormatter(
13  *   id = "datetime_default",
14  *   label = @Translation("Default"),
15  *   field_types = {
16  *     "datetime"
17  *   }
18  * )
19  */
20 class DateTimeDefaultFormatter extends DateTimeFormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'format_type' => 'medium',
28     ] + parent::defaultSettings();
29   }
30
31   /**
32    * {@inheritdoc}
33    */
34   public function viewElements(FieldItemListInterface $items, $langcode) {
35     $elements = [];
36
37     foreach ($items as $delta => $item) {
38       $output = '';
39       $iso_date = '';
40
41       if ($item->date) {
42         /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
43         $date = $item->date;
44
45         if ($this->getFieldSetting('datetime_type') == 'date') {
46           // A date without time will pick up the current time, use the default.
47           datetime_date_default_time($date);
48         }
49
50         // Create the ISO date in Universal Time.
51         $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
52
53         $this->setTimeZone($date);
54
55         $output = $this->formatDate($date);
56       }
57
58       // Display the date using theme datetime.
59       $elements[$delta] = [
60         '#cache' => [
61           'contexts' => [
62             'timezone',
63           ],
64         ],
65         '#theme' => 'time',
66         '#text' => $output,
67         '#html' => FALSE,
68         '#attributes' => [
69           'datetime' => $iso_date,
70         ],
71       ];
72       if (!empty($item->_attributes)) {
73         $elements[$delta]['#attributes'] += $item->_attributes;
74         // Unset field item attributes since they have been included in the
75         // formatter output and should not be rendered in the field template.
76         unset($item->_attributes);
77       }
78     }
79
80     return $elements;
81
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   protected function formatDate($date) {
88     $format_type = $this->getSetting('format_type');
89     $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName();
90     return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL);
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function settingsForm(array $form, FormStateInterface $form_state) {
97     $form = parent::settingsForm($form, $form_state);
98
99     $time = new DrupalDateTime();
100     $format_types = $this->dateFormatStorage->loadMultiple();
101     $options = [];
102     foreach ($format_types as $type => $type_info) {
103       $format = $this->dateFormatter->format($time->getTimestamp(), $type);
104       $options[$type] = $type_info->label() . ' (' . $format . ')';
105     }
106
107     $form['format_type'] = [
108       '#type' => 'select',
109       '#title' => t('Date format'),
110       '#description' => t("Choose a format for displaying the date. Be sure to set a format appropriate for the field, i.e. omitting time for a field that only has a date."),
111       '#options' => $options,
112       '#default_value' => $this->getSetting('format_type'),
113     ];
114
115     return $form;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function settingsSummary() {
122     $summary = parent::settingsSummary();
123
124     $date = new DrupalDateTime();
125     $summary[] = t('Format: @display', ['@display' => $this->formatDate($date, $this->getFormatSettings())]);
126
127     return $summary;
128   }
129
130 }