3 namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
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;
15 * Plugin implementation of the 'timestamp' formatter.
19 * label = @Translation("Default"),
27 class TimestampFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
30 * The date formatter service.
32 * @var \Drupal\Core\Datetime\DateFormatterInterface
34 protected $dateFormatter;
37 * The date format entity storage.
39 * @var \Drupal\Core\Entity\EntityStorageInterface
41 protected $dateFormatStorage;
44 * Constructs a new TimestampFormatter.
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
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.
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);
68 $this->dateFormatter = $date_formatter;
69 $this->dateFormatStorage = $date_format_storage;
75 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $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')
92 public static function defaultSettings() {
94 'date_format' => 'medium',
95 'custom_date_format' => '',
97 ] + parent::defaultSettings();
103 public function settingsForm(array $form, FormStateInterface $form_state) {
104 $elements = parent::settingsForm($form, $form_state);
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)]);
112 $date_formats['custom'] = $this->t('Custom');
114 $elements['date_format'] = [
116 '#title' => $this->t('Date format'),
117 '#options' => $date_formats,
118 '#default_value' => $this->getSetting('date_format') ?: 'medium',
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') ?: '',
128 $elements['custom_date_format']['#states']['visible'][] = [
129 ':input[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][date_format]"]' => ['value' => 'custom'],
132 $elements['timezone'] = [
134 '#title' => $this->t('Time zone'),
135 '#options' => ['' => $this->t('- Default site/user time zone -')] + system_time_zones(FALSE, TRUE),
136 '#default_value' => $this->getSetting('timezone'),
145 public function settingsSummary() {
146 $summary = parent::settingsSummary();
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]);
153 if ($timezone = $this->getSetting('timezone')) {
154 $summary[] = $this->t('Time zone: @timezone', ['@timezone' => $timezone]);
163 public function viewElements(FieldItemListInterface $items, $langcode) {
166 $date_format = $this->getSetting('date_format');
167 $custom_date_format = '';
168 $timezone = $this->getSetting('timezone') ?: NULL;
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') {
177 foreach ($items as $delta => $item) {
178 $elements[$delta] = [
184 '#markup' => $this->dateFormatter->format($item->value, $date_format, $custom_date_format, $timezone, $langcode),