You are here

protected function DateTimeFromStringEnhancer::doTransform in JSON:API Extras 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php \Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer\DateTimeFromStringEnhancer::doTransform()

File

src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php, line 46

Class

DateTimeFromStringEnhancer
Perform additional manipulations to datetime fields.

Namespace

Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer

Code

protected function doTransform($data, Context $context) {
  $reformat = function ($input) {
    $date = new \DateTime($input);

    // Adjust the date for storage.
    $storage_timezone = new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE);
    $date
      ->setTimezone($storage_timezone);
    $output = $date
      ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
    return $output;
  };
  $result = is_array($data) ? array_map($reformat, $data) : $reformat($data);
  return $result;
}