summaryrefslogtreecommitdiffstats
path: root/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
diff options
context:
space:
mode:
authorarshad2017-05-01 15:19:34 +0200
committerMateu Aguiló Bosch2017-05-01 15:19:34 +0200
commit31bef9c89bc5c11077c20b00386f67a527c98cb3 (patch)
treeb505e2990e8087e8e746a4b7b61ce64d7c8bce8a /src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
parent67417178940fea840adfdb51aca3e4dd5142f6ae (diff)
feat(DX): Move field enhancers under a Plugin\jsonapi\FieldEnhancer namespace (#2874324 by arshadcn, e0ipso)
Diffstat (limited to 'src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php')
-rw-r--r--src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php110
1 files changed, 110 insertions, 0 deletions
diff --git a/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
new file mode 100644
index 0000000..4f5aee2
--- /dev/null
+++ b/src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php
@@ -0,0 +1,110 @@
+<?php
+
+namespace Drupal\jsonapi_extras\Plugin\jsonapi\FieldEnhancer;
+
+use Drupal\Core\Annotation\Translation;
+use Drupal\Core\Plugin\PluginBase;
+use Drupal\Core\StringTranslation\StringTranslationTrait;
+use Drupal\jsonapi_extras\Annotation\ResourceFieldEnhancer;
+use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerInterface;
+
+/**
+ * Perform additional manipulations to date fields.
+ *
+ * @ResourceFieldEnhancer(
+ * id = "date_time",
+ * label = @Translation("Date Time"),
+ * description = @Translation("Transform a data format into anothed different based the configured date format.")
+ * )
+ */
+class DateTimeEnhancer extends PluginBase implements ResourceFieldEnhancerInterface {
+
+ /**
+ * Holds the plugin configuration.
+ *
+ * @var array
+ */
+ protected $configuration;
+
+ /**
+ * {@inheritdoc}
+ */
+ public function calculateDependencies() {
+ // TODO: This should have a dependency on the resource_config configuration entity.
+ return [];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'dateTimeFormat' => \DateTime::ISO8601,
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getConfiguration() {
+ return $this->configuration
+ ? $this->configuration
+ : $this->setConfiguration([]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setConfiguration(array $configuration) {
+ $this->configuration = $configuration + $this->defaultConfiguration();
+ return $this->configuration;
+ }
+
+ /**
+ * {@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');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getJsonSchema() {
+ return [
+ 'type' => 'string',
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getSettingsForm(array $resource_field_info) {
+ $settings = empty($resource_field_info['settings'])
+ ? $this->getConfiguration()
+ : $resource_field_info['settings'];
+
+ return [
+ 'dateTimeFormat' => [
+ '#type' => 'textfield',
+ '#title' => $this->t('Format'),
+ '#description' => $this->t('Use a valid date format.'),
+ '#default_value' => $settings['dateTimeFormat'],
+ ],
+ ];
+ }
+
+}