98b1ea3857f9767351dae171a77e5c69eec18875
[yaffs-website] / web / core / modules / datetime / src / Plugin / Field / FieldFormatter / DateTimeFormatterBase.php
1 <?php
2
3 namespace Drupal\datetime\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Datetime\DrupalDateTime;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Field\FieldDefinitionInterface;
9 use Drupal\Core\Field\FieldItemListInterface;
10 use Drupal\Core\Field\FormatterBase;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
13 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Base class for 'DateTime Field formatter' plugin implementations.
18  */
19 abstract class DateTimeFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {
20
21   /**
22    * The date formatter service.
23    *
24    * @var \Drupal\Core\Datetime\DateFormatterInterface
25    */
26   protected $dateFormatter;
27
28   /**
29    * The date format entity storage.
30    *
31    * @var \Drupal\Core\Entity\EntityStorageInterface
32    */
33   protected $dateFormatStorage;
34
35   /**
36    * Constructs a new DateTimeDefaultFormatter.
37    *
38    * @param string $plugin_id
39    *   The plugin_id for the formatter.
40    * @param mixed $plugin_definition
41    *   The plugin implementation definition.
42    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
43    *   The definition of the field to which the formatter is associated.
44    * @param array $settings
45    *   The formatter settings.
46    * @param string $label
47    *   The formatter label display setting.
48    * @param string $view_mode
49    *   The view mode.
50    * @param array $third_party_settings
51    *   Third party settings.
52    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
53    *   The date formatter service.
54    * @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
55    *   The date format entity storage.
56    */
57   public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
58     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
59
60     $this->dateFormatter = $date_formatter;
61     $this->dateFormatStorage = $date_format_storage;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
68     return new static(
69       $plugin_id,
70       $plugin_definition,
71       $configuration['field_definition'],
72       $configuration['settings'],
73       $configuration['label'],
74       $configuration['view_mode'],
75       $configuration['third_party_settings'],
76       $container->get('date.formatter'),
77       $container->get('entity.manager')->getStorage('date_format')
78     );
79   }
80
81   /**
82    * {@inheritdoc}
83    */
84   public static function defaultSettings() {
85     return [
86       'timezone_override' => '',
87     ] + parent::defaultSettings();
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function settingsForm(array $form, FormStateInterface $form_state) {
94     $form = parent::settingsForm($form, $form_state);
95
96     $form['timezone_override'] = [
97       '#type' => 'select',
98       '#title' => $this->t('Time zone override'),
99       '#description' => $this->t('The time zone selected here will always be used'),
100       '#options' => system_time_zones(TRUE, TRUE),
101       '#default_value' => $this->getSetting('timezone_override'),
102     ];
103
104     return $form;
105   }
106
107   /**
108    * {@inheritdoc}
109    */
110   public function settingsSummary() {
111     $summary = parent::settingsSummary();
112
113     if ($override = $this->getSetting('timezone_override')) {
114       $summary[] = $this->t('Time zone: @timezone', ['@timezone' => $override]);
115     }
116
117     return $summary;
118   }
119
120   /**
121    * {@inheritdoc}
122    */
123   public function viewElements(FieldItemListInterface $items, $langcode) {
124     $elements = [];
125
126     foreach ($items as $delta => $item) {
127       if ($item->date) {
128         /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
129         $date = $item->date;
130         $elements[$delta] = $this->buildDateWithIsoAttribute($date);
131
132         if (!empty($item->_attributes)) {
133           $elements[$delta]['#attributes'] += $item->_attributes;
134           // Unset field item attributes since they have been included in the
135           // formatter output and should not be rendered in the field template.
136           unset($item->_attributes);
137         }
138       }
139     }
140
141     return $elements;
142   }
143
144   /**
145    * Creates a formatted date value as a string.
146    *
147    * @param object $date
148    *   A date object.
149    *
150    * @return string
151    *   A formatted date string using the chosen format.
152    */
153   abstract protected function formatDate($date);
154
155   /**
156    * Sets the proper time zone on a DrupalDateTime object for the current user.
157    *
158    * A DrupalDateTime object loaded from the database will have the UTC time
159    * zone applied to it.  This method will apply the time zone for the current
160    * user, based on system and user settings.
161    *
162    * @see drupal_get_user_timezone()
163    *
164    * @param \Drupal\Core\Datetime\DrupalDateTime $date
165    *   A DrupalDateTime object.
166    */
167   protected function setTimeZone(DrupalDateTime $date) {
168     if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
169       // A date without time has no timezone conversion.
170       $timezone = DATETIME_STORAGE_TIMEZONE;
171     }
172     else {
173       $timezone = drupal_get_user_timezone();
174     }
175     $date->setTimeZone(timezone_open($timezone));
176   }
177
178   /**
179    * Gets a settings array suitable for DrupalDateTime::format().
180    *
181    * @return array
182    *   The settings array that can be passed to DrupalDateTime::format().
183    */
184   protected function getFormatSettings() {
185     $settings = [];
186
187     if ($this->getSetting('timezone_override') != '') {
188       $settings['timezone'] = $this->getSetting('timezone_override');
189     }
190
191     return $settings;
192   }
193
194   /**
195    * Creates a render array from a date object.
196    *
197    * @param \Drupal\Core\Datetime\DrupalDateTime $date
198    *   A date object.
199    *
200    * @return array
201    *   A render array.
202    */
203   protected function buildDate(DrupalDateTime $date) {
204     if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
205       // A date without time will pick up the current time, use the default.
206       datetime_date_default_time($date);
207     }
208     $this->setTimeZone($date);
209
210     $build = [
211       '#markup' => $this->formatDate($date),
212       '#cache' => [
213         'contexts' => [
214           'timezone',
215         ],
216       ],
217     ];
218
219     return $build;
220   }
221
222   /**
223    * Creates a render array from a date object with ISO date attribute.
224    *
225    * @param \Drupal\Core\Datetime\DrupalDateTime $date
226    *   A date object.
227    *
228    * @return array
229    *   A render array.
230    */
231   protected function buildDateWithIsoAttribute(DrupalDateTime $date) {
232     if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
233       // A date without time will pick up the current time, use the default.
234       datetime_date_default_time($date);
235     }
236
237     // Create the ISO date in Universal Time.
238     $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
239
240     $this->setTimeZone($date);
241
242     $build = [
243       '#theme' => 'time',
244       '#text' => $this->formatDate($date),
245       '#html' => FALSE,
246       '#attributes' => [
247         'datetime' => $iso_date,
248       ],
249       '#cache' => [
250         'contexts' => [
251           'timezone',
252         ],
253       ],
254     ];
255
256     return $build;
257   }
258
259 }