summaryrefslogtreecommitdiffstats
path: root/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
blob: e53b8fd0d8dfedb944d44e3ccbcbe43f28efd5d8 (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
<?php

namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;

use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;

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

  /**
   * {@inheritdoc}
   */
  public function postProcess($value) {
    $date = new \DateTime();
    $date->setTimestamp($value);
    $configuration = $this->getConfiguration();

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

  /**
   * {@inheritdoc}
   */
  public function prepareForInput($value) {
    $date = new \DateTime($value);

    return (int) $date->format('U');
  }

}