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\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;
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),
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]);
121 * Creates a formatted date value as a string.
123 * @param object $date
127 * A formatted date string using the chosen format.
129 abstract protected function formatDate($date);
132 * Sets the proper time zone on a DrupalDateTime object for the current user.
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.
138 * @see drupal_get_user_timezone()
140 * @param \Drupal\Core\Datetime\DrupalDateTime $date
141 * A DrupalDateTime object.
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;
149 $timezone = drupal_get_user_timezone();
151 $date->setTimeZone(timezone_open($timezone));
155 * Gets a settings array suitable for DrupalDateTime::format().
158 * The settings array that can be passed to DrupalDateTime::format().
160 protected function getFormatSettings() {
163 if ($this->getSetting('timezone_override') != '') {
164 $settings['timezone'] = $this->getSetting('timezone_override');