Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / lib / Drupal / Core / Datetime / Plugin / Field / FieldWidget / TimestampDatetimeWidget.php
1 <?php
2
3 namespace Drupal\Core\Datetime\Plugin\Field\FieldWidget;
4
5 use Drupal\Core\Datetime\DrupalDateTime;
6 use Drupal\Core\Datetime\Element\Datetime;
7 use Drupal\Core\Datetime\Entity\DateFormat;
8 use Drupal\Core\Field\FieldItemListInterface;
9 use Drupal\Core\Field\WidgetBase;
10 use Drupal\Core\Form\FormStateInterface;
11
12 /**
13  * Plugin implementation of the 'datetime timestamp' widget.
14  *
15  * @FieldWidget(
16  *   id = "datetime_timestamp",
17  *   label = @Translation("Datetime Timestamp"),
18  *   field_types = {
19  *     "timestamp",
20  *     "created",
21  *   }
22  * )
23  */
24 class TimestampDatetimeWidget extends WidgetBase {
25
26   /**
27    * {@inheritdoc}
28    */
29   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
30     $date_format = DateFormat::load('html_date')->getPattern();
31     $time_format = DateFormat::load('html_time')->getPattern();
32     $default_value = isset($items[$delta]->value) ? DrupalDateTime::createFromTimestamp($items[$delta]->value) : '';
33     $element['value'] = $element + [
34       '#type' => 'datetime',
35       '#default_value' => $default_value,
36       '#date_year_range' => '1902:2037',
37     ];
38     $element['value']['#description'] = $this->t('Format: %format. Leave blank to use the time of form submission.', ['%format' => Datetime::formatExample($date_format . ' ' . $time_format)]);
39
40     return $element;
41   }
42
43   /**
44    * {@inheritdoc}
45    */
46   public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
47     foreach ($values as &$item) {
48       // @todo The structure is different whether access is denied or not, to
49       //   be fixed in https://www.drupal.org/node/2326533.
50       if (isset($item['value']) && $item['value'] instanceof DrupalDateTime) {
51         $date = $item['value'];
52       }
53       elseif (isset($item['value']['object']) && $item['value']['object'] instanceof DrupalDateTime) {
54         $date = $item['value']['object'];
55       }
56       else {
57         $date = new DrupalDateTime();
58       }
59       $item['value'] = $date->getTimestamp();
60     }
61     return $values;
62   }
63
64 }