Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\datetime;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
|
Chris@0
|
6 use Drupal\Core\Datetime\DrupalDateTime;
|
Chris@0
|
7 use Drupal\Core\TypedData\DataDefinitionInterface;
|
Chris@0
|
8 use Drupal\Core\TypedData\TypedDataInterface;
|
Chris@0
|
9 use Drupal\Core\TypedData\TypedData;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * A computed property for dates of date time field items.
|
Chris@0
|
13 *
|
Chris@0
|
14 * Required settings (below the definition's 'settings' key) are:
|
Chris@0
|
15 * - date source: The date property containing the to be computed date.
|
Chris@0
|
16 */
|
Chris@0
|
17 class DateTimeComputed extends TypedData {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Cached computed date.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \DateTime|null
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $date = NULL;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * {@inheritdoc}
|
Chris@0
|
28 */
|
Chris@0
|
29 public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
|
Chris@0
|
30 parent::__construct($definition, $name, $parent);
|
Chris@0
|
31 if (!$definition->getSetting('date source')) {
|
Chris@0
|
32 throw new \InvalidArgumentException("The definition's 'date source' key has to specify the name of the date property to be computed.");
|
Chris@0
|
33 }
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public function getValue($langcode = NULL) {
|
Chris@0
|
40 if ($this->date !== NULL) {
|
Chris@0
|
41 return $this->date;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /** @var \Drupal\Core\Field\FieldItemInterface $item */
|
Chris@0
|
45 $item = $this->getParent();
|
Chris@0
|
46 $value = $item->{($this->definition->getSetting('date source'))};
|
Chris@0
|
47
|
Chris@0
|
48 $datetime_type = $item->getFieldDefinition()->getSetting('datetime_type');
|
Chris@0
|
49 $storage_format = $datetime_type === DateTimeItem::DATETIME_TYPE_DATE ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
Chris@0
|
50 try {
|
Chris@0
|
51 $date = DrupalDateTime::createFromFormat($storage_format, $value, DATETIME_STORAGE_TIMEZONE);
|
Chris@0
|
52 if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
|
Chris@0
|
53 $this->date = $date;
|
Chris@0
|
54 // If the format did not include an explicit time portion, then the
|
Chris@0
|
55 // time will be set from the current time instead. For consistency, we
|
Chris@0
|
56 // set the time to 12:00:00 UTC for date-only fields. This is used so
|
Chris@0
|
57 // that the local date portion is the same, across nearly all time
|
Chris@0
|
58 // zones.
|
Chris@0
|
59 // @see datetime_date_default_time()
|
Chris@0
|
60 // @see http://php.net/manual/en/datetime.createfromformat.php
|
Chris@0
|
61 // @todo Update comment and/or code per the chosen solution in
|
Chris@0
|
62 // https://www.drupal.org/node/2830094
|
Chris@0
|
63 if ($datetime_type === DateTimeItem::DATETIME_TYPE_DATE) {
|
Chris@0
|
64 $this->date->setTime(12, 0, 0);
|
Chris@0
|
65 }
|
Chris@0
|
66 }
|
Chris@0
|
67 }
|
Chris@0
|
68 catch (\Exception $e) {
|
Chris@0
|
69 // @todo Handle this.
|
Chris@0
|
70 }
|
Chris@0
|
71 return $this->date;
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function setValue($value, $notify = TRUE) {
|
Chris@0
|
78 $this->date = $value;
|
Chris@0
|
79 // Notify the parent of any changes.
|
Chris@0
|
80 if ($notify && isset($this->parent)) {
|
Chris@0
|
81 $this->parent->onChange($this->name);
|
Chris@0
|
82 }
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 }
|