Security update for Core, with self-updated composer
[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     // @todo Evaluate removing this method in
36     // https://www.drupal.org/node/2793143 to determine if the behavior and
37     // markup in the base class implementation can be used instead.
38     $elements = [];
39
40     foreach ($items as $delta => $item) {
41       if (!empty($item->date)) {
42         /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
43         $date = $item->date;
44
45         $elements[$delta] = $this->buildDate($date);
46       }
47     }
48
49     return $elements;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function formatDate($date) {
56     $format = $this->getSetting('date_format');
57     $timezone = $this->getSetting('timezone_override');
58     return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL);
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   public function settingsForm(array $form, FormStateInterface $form_state) {
65     $form = parent::settingsForm($form, $form_state);
66
67     $form['date_format'] = [
68       '#type' => 'textfield',
69       '#title' => $this->t('Date/time format'),
70       '#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
71       '#default_value' => $this->getSetting('date_format'),
72     ];
73
74     return $form;
75   }
76
77   /**
78    * {@inheritdoc}
79    */
80   public function settingsSummary() {
81     $summary = parent::settingsSummary();
82
83     $date = new DrupalDateTime();
84     $this->setTimeZone($date);
85     $summary[] = $date->format($this->getSetting('date_format'), $this->getFormatSettings());
86
87     return $summary;
88   }
89
90 }