Version 1
[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\FormatterBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
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),
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    * Creates a formatted date value as a string.
122    *
123    * @param object $date
124    *   A date object.
125    *
126    * @return string
127    *   A formatted date string using the chosen format.
128    */
129   abstract protected function formatDate($date);
130
131   /**
132    * Sets the proper time zone on a DrupalDateTime object for the current user.
133    *
134    * A DrupalDateTime object loaded from the database will have the UTC time
135    * zone applied to it.  This method will apply the time zone for the current
136    * user, based on system and user settings.
137    *
138    * @see drupal_get_user_timezone()
139    *
140    * @param \Drupal\Core\Datetime\DrupalDateTime $date
141    *   A DrupalDateTime object.
142    */
143   protected function setTimeZone(DrupalDateTime $date) {
144     if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
145       // A date without time has no timezone conversion.
146       $timezone = DATETIME_STORAGE_TIMEZONE;
147     }
148     else {
149       $timezone = drupal_get_user_timezone();
150     }
151     $date->setTimeZone(timezone_open($timezone));
152   }
153
154   /**
155    * Gets a settings array suitable for DrupalDateTime::format().
156    *
157    * @return array
158    *   The settings array that can be passed to DrupalDateTime::format().
159    */
160   protected function getFormatSettings() {
161     $settings = [];
162
163     if ($this->getSetting('timezone_override') != '') {
164       $settings['timezone'] = $this->getSetting('timezone_override');
165     }
166
167     return $settings;
168   }
169
170 }