DateTimeComputed.php 2.7 KB

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