3 namespace Drupal\views\Plugin\views\field;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Form\FormStateInterface;
7 use Drupal\views\ResultRow;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9 use Drupal\Core\Datetime\DateFormatterInterface;
12 * A handler to provide proper displays for dates.
14 * @ingroup views_field_handlers
18 class Date extends FieldPluginBase {
21 * The date formatter service.
23 * @var \Drupal\Core\Datetime\DateFormatterInterface
25 protected $dateFormatter;
28 * The date format storage.
30 * @var \Drupal\Core\Entity\EntityStorageInterface
32 protected $dateFormatStorage;
35 * Constructs a new Date object.
37 * @param array $configuration
38 * A configuration array containing information about the plugin instance.
39 * @param string $plugin_id
40 * The plugin ID for the plugin instance.
41 * @param mixed $plugin_definition
42 * The plugin implementation definition.
43 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
44 * The date formatter service.
45 * @param \Drupal\Core\Entity\EntityStorageInterface $date_format_storage
46 * The date format storage.
48 public function __construct(array $configuration, $plugin_id, $plugin_definition, DateFormatterInterface $date_formatter, EntityStorageInterface $date_format_storage) {
49 parent::__construct($configuration, $plugin_id, $plugin_definition);
51 $this->dateFormatter = $date_formatter;
52 $this->dateFormatStorage = $date_format_storage;
58 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
63 $container->get('date.formatter'),
64 $container->get('entity.manager')->getStorage('date_format')
71 protected function defineOptions() {
72 $options = parent::defineOptions();
74 $options['date_format'] = ['default' => 'small'];
75 $options['custom_date_format'] = ['default' => ''];
76 $options['timezone'] = ['default' => ''];
84 public function buildOptionsForm(&$form, FormStateInterface $form_state) {
87 foreach ($this->dateFormatStorage->loadMultiple() as $machine_name => $value) {
88 $date_formats[$machine_name] = $this->t('@name format: @date', ['@name' => $value->label(), '@date' => $this->dateFormatter->format(REQUEST_TIME, $machine_name)]);
91 $form['date_format'] = [
93 '#title' => $this->t('Date format'),
94 '#options' => $date_formats + [
95 'custom' => $this->t('Custom'),
96 'raw time ago' => $this->t('Time ago'),
97 'time ago' => $this->t('Time ago (with "ago" appended)'),
98 'raw time hence' => $this->t('Time hence'),
99 'time hence' => $this->t('Time hence (with "hence" appended)'),
100 'raw time span' => $this->t('Time span (future dates have "-" prepended)'),
101 'inverse time span' => $this->t('Time span (past dates have "-" prepended)'),
102 'time span' => $this->t('Time span (with "ago/hence" appended)'),
104 '#default_value' => isset($this->options['date_format']) ? $this->options['date_format'] : 'small',
106 $form['custom_date_format'] = [
107 '#type' => 'textfield',
108 '#title' => $this->t('Custom date format'),
109 '#description' => $this->t('If "Custom", see <a href="http://us.php.net/manual/en/function.date.php" target="_blank">the PHP docs</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.'),
110 '#default_value' => isset($this->options['custom_date_format']) ? $this->options['custom_date_format'] : '',
112 // Setup #states for all possible date_formats on the custom_date_format form element.
113 foreach (['custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'] as $custom_date_possible) {
114 $form['custom_date_format']['#states']['visible'][] = [
115 ':input[name="options[date_format]"]' => ['value' => $custom_date_possible],
118 $form['timezone'] = [
120 '#title' => $this->t('Timezone'),
121 '#description' => $this->t('Timezone to be used for date output.'),
122 '#options' => ['' => $this->t('- Default site/user timezone -')] + system_time_zones(FALSE, TRUE),
123 '#default_value' => $this->options['timezone'],
125 foreach (array_merge(['custom'], array_keys($date_formats)) as $timezone_date_formats) {
126 $form['timezone']['#states']['visible'][] = [
127 ':input[name="options[date_format]"]' => ['value' => $timezone_date_formats],
131 parent::buildOptionsForm($form, $form_state);
137 public function render(ResultRow $values) {
138 $value = $this->getValue($values);
139 $format = $this->options['date_format'];
140 if (in_array($format, ['custom', 'raw time ago', 'time ago', 'raw time hence', 'time hence', 'raw time span', 'time span', 'raw time span', 'inverse time span', 'time span'])) {
141 $custom_format = $this->options['custom_date_format'];
145 $timezone = !empty($this->options['timezone']) ? $this->options['timezone'] : NULL;
146 // Will be positive for a datetime in the past (ago), and negative for a
147 // datetime in the future (hence).
148 $time_diff = REQUEST_TIME - $value;
151 return $this->dateFormatter->formatTimeDiffSince($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2]);
154 return $this->t('%time ago', ['%time' => $this->dateFormatter->formatTimeDiffSince($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);
156 case 'raw time hence':
157 return $this->dateFormatter->formatTimeDiffUntil($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2]);
160 return $this->t('%time hence', ['%time' => $this->dateFormatter->formatTimeDiffUntil($value, ['granularity' => is_numeric($custom_format) ? $custom_format : 2])]);
162 case 'raw time span':
163 return ($time_diff < 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
165 case 'inverse time span':
166 return ($time_diff > 0 ? '-' : '') . $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
169 $time = $this->dateFormatter->formatTimeDiffSince($value, ['strict' => FALSE, 'granularity' => is_numeric($custom_format) ? $custom_format : 2]);
170 return ($time_diff < 0) ? $this->t('%time hence', ['%time' => $time]) : $this->t('%time ago', ['%time' => $time]);
173 if ($custom_format == 'r') {
174 return format_date($value, $format, $custom_format, $timezone, 'en');
176 return format_date($value, $format, $custom_format, $timezone);
179 return format_date($value, $format, '', $timezone);