Skip to content
DateTimeFromStringEnhancer.php 1.77 KiB
Newer Older
<?php

namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;

use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;

/**
 * Perform additional manipulations to datetime fields.
 *
 * @ResourceFieldEnhancer(
 *   id = "date_time_from_string",
 *   label = @Translation("Date Time (Date Time field)"),
 *   description = @Translation("Formats a date based the configured date format for date fields.")
 * )
 */
class DateTimeFromStringEnhancer extends DateTimeEnhancerBase {

  /**
   * {@inheritdoc}
   */
  public function postProcess($value) {
    if (is_array($value)) {
        return array_map([$this, 'processSingleValue'], $value);
    }

    return $this->processSingleValue($value);
  }

  /**
   * Process a single value
   *
   * @param $value
   * @return string
   */
  protected function processSingleValue($value) {
    $storage_timezone = new \DateTimezone(DATETIME_STORAGE_TIMEZONE);
    $date = new \DateTime($value, $storage_timezone);

    $configuration = $this->getConfiguration();

    $output_timezone = new \DateTimezone(drupal_get_user_timezone());
    $date->setTimezone($output_timezone);

    return $date->format($configuration['dateTimeFormat']);
  }

  /**
   * {@inheritdoc}
   */
  public function prepareForInput($value) {
    if (is_array($value)) {
        return array_map([$this, 'prepareSingleValue'], $value);
    }

    return $this->prepareSingleValue($value);
  }

  /**
   * Prepare a single value for input.
   *
   * @param $value
   * @return string
   */
  protected function prepareSingleValue($value) {
    $date = new \DateTime($value);

    // Adjust the date for storage.
    $storage_timezone = new \DateTimezone(DATETIME_STORAGE_TIMEZONE);
    $date->setTimezone($storage_timezone);

    return $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
  }

}