You are here

public function DateTimeIso8601Normalizer::normalize in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php \Drupal\serialization\Normalizer\DateTimeIso8601Normalizer::normalize()

Overrides DateTimeNormalizer::normalize

File

core/modules/serialization/src/Normalizer/DateTimeIso8601Normalizer.php, line 40

Class

DateTimeIso8601Normalizer
Converts values for the DateTimeIso8601 data type to RFC3339.

Namespace

Drupal\serialization\Normalizer

Code

public function normalize($datetime, $format = NULL, array $context = []) {
  assert($datetime instanceof DateTimeIso8601);
  $field_item = $datetime
    ->getParent();

  // @todo Remove this in https://www.drupal.org/project/drupal/issues/2958416.
  if ($field_item instanceof DateTimeItem && $field_item
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
    $drupal_date_time = $datetime
      ->getDateTime();
    if ($drupal_date_time === NULL) {
      return $drupal_date_time;
    }
    return $drupal_date_time
      ->format($this->allowedFormats['date-only']);
  }
  return parent::normalize($datetime, $format, $context);
}