3 namespace Drupal\datetime\Plugin\Field\FieldFormatter;
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;
17 * Base class for 'DateTime Field formatter' plugin implementations.
19 abstract class DateTimeFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {
22 * The date formatter service.
24 * @var \Drupal\Core\Datetime\DateFormatterInterface
26 protected $dateFormatter;
29 * The date format entity storage.
31 * @var \Drupal\Core\Entity\EntityStorageInterface
33 protected $dateFormatStorage;
36 * Constructs a new DateTimeDefaultFormatter.
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
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.
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);
60 $this->dateFormatter = $date_formatter;
61 $this->dateFormatStorage = $date_format_storage;
67 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
84 public static function defaultSettings() {
86 'timezone_override' => '',
87 ] + parent::defaultSettings();
93 public function settingsForm(array $form, FormStateInterface $form_state) {
94 $form = parent::settingsForm($form, $form_state);
96 $form['timezone_override'] = [
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'),
110 public function settingsSummary() {
111 $summary = parent::settingsSummary();
113 if ($override = $this->getSetting('timezone_override')) {
114 $summary[] = $this->t('Time zone: @timezone', ['@timezone' => $override]);
123 public function viewElements(FieldItemListInterface $items, $langcode) {
126 foreach ($items as $delta => $item) {
128 /** @var \Drupal\Core\Datetime\DrupalDateTime $date */
130 $elements[$delta] = $this->buildDateWithIsoAttribute($date);
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);
145 * Creates a formatted date value as a string.
147 * @param object $date
151 * A formatted date string using the chosen format.
153 abstract protected function formatDate($date);
156 * Sets the proper time zone on a DrupalDateTime object for the current user.
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.
162 * @see drupal_get_user_timezone()
164 * @param \Drupal\Core\Datetime\DrupalDateTime $date
165 * A DrupalDateTime object.
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;
173 $timezone = drupal_get_user_timezone();
175 $date->setTimeZone(timezone_open($timezone));
179 * Gets a settings array suitable for DrupalDateTime::format().
182 * The settings array that can be passed to DrupalDateTime::format().
184 protected function getFormatSettings() {
187 if ($this->getSetting('timezone_override') != '') {
188 $settings['timezone'] = $this->getSetting('timezone_override');
195 * Creates a render array from a date object.
197 * @param \Drupal\Core\Datetime\DrupalDateTime $date
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);
208 $this->setTimeZone($date);
211 '#markup' => $this->formatDate($date),
223 * Creates a render array from a date object with ISO date attribute.
225 * @param \Drupal\Core\Datetime\DrupalDateTime $date
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);
237 // Create the ISO date in Universal Time.
238 $iso_date = $date->format("Y-m-d\TH:i:s") . 'Z';
240 $this->setTimeZone($date);
244 '#text' => $this->formatDate($date),
247 'datetime' => $iso_date,