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