summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Plugin/DateTimeEnhancerBase.php56
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php46
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php46
3 files changed, 107 insertions, 41 deletions
diff --git a/src/Plugin/DateTimeEnhancerBase.php b/src/Plugin/DateTimeEnhancerBase.php
new file mode 100644
index 0000000..62e46b0
--- /dev/null
+++ b/src/Plugin/DateTimeEnhancerBase.php
@@ -0,0 +1,56 @@
+<?php
+
+namespace Drupal\jsonapi_extras\Plugin;
+
+/**
+ * Base class for date and time based resourceFieldEnhancer plugins.
+ */
+abstract class DateTimeEnhancerBase extends ResourceFieldEnhancerBase {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'dateTimeFormat' => \DateTime::ISO8601,
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ abstract public function postProcess($value);
+
+ /**
+ * {@inheritdoc}
+ */
+ abstract public function prepareForInput($value);
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getJsonSchema() {
+ return [
+ 'type' => 'string',
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getSettingsForm(array $resource_field_info) {
+ $settings = empty($resource_field_info['enhancer']['settings'])
+ ? $this->getConfiguration()
+ : $resource_field_info['enhancer']['settings'];
+
+ return [
+ 'dateTimeFormat' => [
+ '#type' => 'textfield',
+ '#title' => $this->t('Format'),
+ '#description' => $this->t('Use a valid date format.'),
+ '#default_value' => $settings['dateTimeFormat'],
+ ],
+ ];
+ }
+
+}
diff --git a/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
index 6c55ee9..e53b8fd 100644
--- a/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
+++ b/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
@@ -2,27 +2,18 @@
namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
-use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerBase;
+use Drupal\jsonapi_extras\Plugin\DateTimeEnhancerBase;
/**
- * Perform additional manipulations to date fields.
+ * Perform additional manipulations to timestamp fields.
*
* @ResourceFieldEnhancer(
* id = "date_time",
- * label = @Translation("Date Time"),
- * description = @Translation("Formats a date based the configured date format.")
+ * label = @Translation("Date Time (Timestamp field)"),
+ * description = @Translation("Formats a date based the configured date format for timestamp fields.")
* )
*/
-class DateTimeEnhancer extends ResourceFieldEnhancerBase {
-
- /**
- * {@inheritdoc}
- */
- public function defaultConfiguration() {
- return [
- 'dateTimeFormat' => \DateTime::ISO8601,
- ];
- }
+class DateTimeEnhancer extends DateTimeEnhancerBase {
/**
* {@inheritdoc}
@@ -44,31 +35,4 @@ class DateTimeEnhancer extends ResourceFieldEnhancerBase {
return (int) $date->format('U');
}
- /**
- * {@inheritdoc}
- */
- public function getJsonSchema() {
- return [
- 'type' => 'string',
- ];
- }
-
- /**
- * {@inheritdoc}
- */
- public function getSettingsForm(array $resource_field_info) {
- $settings = empty($resource_field_info['enhancer']['settings'])
- ? $this->getConfiguration()
- : $resource_field_info['enhancer']['settings'];
-
- return [
- 'dateTimeFormat' => [
- '#type' => 'textfield',
- '#title' => $this->t('Format'),
- '#description' => $this->t('Use a valid date format.'),
- '#default_value' => $settings['dateTimeFormat'],
- ],
- ];
- }
-
}
diff --git a/src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php
new file mode 100644
index 0000000..2378f79
--- /dev/null
+++ b/src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php
@@ -0,0 +1,46 @@
+<?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) {
+ $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) {
+ $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);
+ }
+
+}