summaryrefslogtreecommitdiffstats
path: root/src/Plugin/jsonapi/FieldEnhancer
diff options
context:
space:
mode:
authorszeidler2017-11-16 00:25:00 +0100
committerMateu Aguiló Bosch2017-11-16 00:25:00 +0100
commit7321bedd2e1d86892e7d610677a4a6bee1ccf7df (patch)
tree64c3b67f9da17d0f1a3c7dc1932db59aaba9da1d /src/Plugin/jsonapi/FieldEnhancer
parent6ba06da89badc2d23da1ec1d25f343ea41f185d1 (diff)
feat(DateTime): Allow DateTime enhancement for non-timestamp fields (#2921612 by szeidler)8.x-1.x
Diffstat (limited to 'src/Plugin/jsonapi/FieldEnhancer')
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php46
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/DateTimeFromStringEnhancer.php46
2 files changed, 51 insertions, 41 deletions
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);
+ }
+
+}