445b7ee70693b0ee7dba000523d93d1d01f19af9
[yaffs-website] / web / core / modules / datetime / src / Plugin / Field / FieldFormatter / DateTimeCustomFormatter.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 'Custom' formatter for 'datetime' fields.
11  *
12  * @FieldFormatter(
13  *   id = "datetime_custom",
14  *   label = @Translation("Custom"),
15  *   field_types = {
16  *     "datetime"
17  *   }
18  *)
19  */
20 class DateTimeCustomFormatter extends DateTimeFormatterBase {
21
22   /**
23    * {@inheritdoc}
24    */
25   public static function defaultSettings() {
26     return [
27       'date_format' => DATETIME_DATETIME_STORAGE_FORMAT,
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       if (!empty($item->date)) {
40         /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
41         $date = $item->date;
42
43         if ($this->getFieldSetting('datetime_type') == 'date') {
44           // A date without time will pick up the current time, use the default.
45           datetime_date_default_time($date);
46         }
47         $this->setTimeZone($date);
48
49         $output = $this->formatDate($date);
50       }
51       $elements[$delta] = [
52         '#markup' => $output,
53         '#cache' => [
54           'contexts' => [
55             'timezone',
56           ],
57         ],
58       ];
59     }
60
61     return $elements;
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   protected function formatDate($date) {
68     $format = $this->getSetting('date_format');
69     $timezone = $this->getSetting('timezone_override');
70     return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function settingsForm(array $form, FormStateInterface $form_state) {
77     $form = parent::settingsForm($form, $form_state);
78
79     $form['date_format'] = [
80       '#type' => 'textfield',
81       '#title' => $this->t('Date/time format'),
82       '#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
83       '#default_value' => $this->getSetting('date_format'),
84     ];
85
86     return $form;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function settingsSummary() {
93     $summary = parent::settingsSummary();
94
95     $date = new DrupalDateTime();
96     $this->setTimeZone($date);
97     $summary[] = $date->format($this->getSetting('date_format'), $this->getFormatSettings());
98
99     return $summary;
100   }
101
102 }