summaryrefslogtreecommitdiffstats
path: root/core/lib/Drupal/Core/TypedData/Plugin/DataType/DateTimeIso8601.php
blob: 3fe189fa97110e323fae3eb8d414f6e2c7498c08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php

namespace Drupal\Core\TypedData\Plugin\DataType;

use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\TypedData\Type\DateTimeInterface;

/**
 * A data type for ISO 8601 date strings.
 *
 * The plain value of this data type is a date string in ISO 8601 format.
 *
 * @DataType(
 *   id = "datetime_iso8601",
 *   label = @Translation("Date")
 * )
 */
class DateTimeIso8601 extends StringData implements DateTimeInterface {

  /**
   * {@inheritdoc}
   */
  public function getDateTime() {
    if ($this->value) {
      if (is_array($this->value)) {
        $datetime = DrupalDateTime::createFromArray($this->value);
      }
      else {
        $datetime = new DrupalDateTime($this->value);
      }
      return $datetime;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setDateTime(DrupalDateTime $dateTime, $notify = TRUE) {
    $this->value = $dateTime->format('c');
    // Notify the parent of any changes.
    if ($notify && isset($this->parent)) {
      $this->parent->onChange($this->name);
    }
  }

}