DateTimeComputed.php 2.7 KB

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