Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldFormatter / TimestampFormatter.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
4
5 use Drupal\Core\Datetime\DateFormatterInterface;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Field\FieldDefinitionInterface;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\Core\Field\FormatterBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13
14 /**
15  * Plugin implementation of the 'timestamp' formatter.
16  *
17  * @FieldFormatter(
18  *   id = "timestamp",
19  *   label = @Translation("Default"),
20  *   field_types = {
21  *     "timestamp",
22  *     "created",
23  *     "changed",
24  *   }
25  * )
26  */
27 class TimestampFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
28
29   /**
30    * The date formatter service.
31    *
32    * @var \Drupal\Core\Datetime\DateFormatterInterface
33    */
34   protected $dateFormatter;
35
36   /**
37    * The date format entity storage.
38    *
39    * @var \Drupal\Core\Entity\EntityStorageInterface
40    */
41   protected $dateFormatStorage;
42
43   /**
44    * Constructs a new TimestampFormatter.
45    *
46    * @param string $plugin_id
47    *   The plugin_id for the formatter.
48    * @param mixed $plugin_definition
49    *   The plugin implementation definition.
50    * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
51    *   The definition of the field to which the formatter is associated.
52    * @param array $settings
53    *   The formatter settings.
54    * @param string $label
55    *   The formatter label display setting.
56    * @param string $view_mode
57    *   The view mode.
58    * @param array $third_party_settings
59    *   Third party settings.
60    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
61    *   The date formatter service.
62    * @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
63    *   The date format storage.
64    */
65   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) {
66     parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
67
68     $this->dateFormatter = $date_formatter;
69     $this->dateFormatStorage = $date_format_storage;
70   }
71
72   /**
73    * {@inheritdoc}
74    */
75   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
76     return new static(
77       $plugin_id,
78       $plugin_definition,
79       $configuration['field_definition'],
80       $configuration['settings'],
81       $configuration['label'],
82       $configuration['view_mode'],
83       $configuration['third_party_settings'],
84       $container->get('date.formatter'),
85       $container->get('entity.manager')->getStorage('date_format')
86     );
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public static function defaultSettings() {
93     return [
94       'date_format' => 'medium',
95       'custom_date_format' => '',
96       'timezone' => '',
97     ] + parent::defaultSettings();
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function settingsForm(array $form, FormStateInterface $form_state) {
104     $elements = parent::settingsForm($form, $form_state);
105
106     $date_formats = [];
107
108     foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) {
109       $date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)]);
110     }
111
112     $date_formats['custom'] = $this->t('Custom');
113
114     $elements['date_format'] = [
115       '#type' => 'select',
116       '#title' => $this->t('Date format'),
117       '#options' => $date_formats,
118       '#default_value' => $this->getSetting('date_format') ?: 'medium',
119     ];
120
121     $elements['custom_date_format'] = [
122       '#type' => 'textfield',
123       '#title' => $this->t('Custom date format'),
124       '#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
125       '#default_value' => $this->getSetting('custom_date_format') ?: '',
126     ];
127
128     $elements['custom_date_format']['#states']['visible'][] = [
129       ':input[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][date_format]"]' => ['value' => 'custom'],
130     ];
131
132     $elements['timezone'] = [
133       '#type' => 'select',
134       '#title' => $this->t('Time zone'),
135       '#options' => ['' => $this->t('- Default site/user time zone -')] + system_time_zones(FALSE),
136       '#default_value' => $this->getSetting('timezone'),
137     ];
138
139     return $elements;
140   }
141
142   /**
143    * {@inheritdoc}
144    */
145   public function settingsSummary() {
146     $summary = parent::settingsSummary();
147
148     $date_format = $this->getSetting('date_format');
149     $summary[] = $this->t('Date format: @date_format', ['@date_format' => $date_format]);
150     if ($this->getSetting('date_format') === 'custom' && ($custom_date_format = $this->getSetting('custom_date_format'))) {
151       $summary[] = $this->t('Custom date format: @custom_date_format', ['@custom_date_format' => $custom_date_format]);
152     }
153     if ($timezone = $this->getSetting('timezone')) {
154       $summary[] = $this->t('Time zone: @timezone', ['@timezone' => $timezone]);
155     }
156
157     return $summary;
158   }
159
160   /**
161    * {@inheritdoc}
162    */
163   public function viewElements(FieldItemListInterface $items, $langcode) {
164     $elements = [];
165
166     $date_format = $this->getSetting('date_format');
167     $custom_date_format = '';
168     $timezone = $this->getSetting('timezone') ?: NULL;
169     $langcode = NULL;
170
171     // If an RFC2822 date format is requested, then the month and day have to
172     // be in English. @see http://www.faqs.org/rfcs/rfc2822.html
173     if ($date_format === 'custom' && ($custom_date_format = $this->getSetting('custom_date_format')) === 'r') {
174       $langcode = 'en';
175     }
176
177     foreach ($items as $delta => $item) {
178       $elements[$delta] = [
179         '#cache' => [
180           'contexts' => [
181             'timezone',
182           ],
183         ],
184         '#markup' => $this->dateFormatter->format($item->value, $date_format, $custom_date_format, $timezone, $langcode),
185       ];
186     }
187
188     return $elements;
189   }
190
191 }