diff options
author | arshad | 2017-05-01 13:19:34 (GMT) |
---|---|---|
committer | Mateu Aguiló Bosch | 2017-05-01 13:19:34 (GMT) |
commit | 31bef9c89bc5c11077c20b00386f67a527c98cb3 (patch) | |
tree | b505e2990e8087e8e746a4b7b61ce64d7c8bce8a /src/Plugin/jsonapi | |
parent | 67417178940fea840adfdb51aca3e4dd5142f6ae (diff) |
feat(DX): Move field enhancers under a Plugin\jsonapi\FieldEnhancer namespace (#2874324 by arshadcn, e0ipso)
Diffstat (limited to 'src/Plugin/jsonapi')
-rw-r--r-- | src/Plugin/jsonapi/FieldEnhancer/DateTimeEnhancer.php | 110 | ||||
-rw-r--r-- | src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php | 123 |
2 files changed, 233 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'], + ], + ]; + } + +} diff --git a/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php b/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php new file mode 100644 index 0000000..094f9a5 --- /dev/null +++ b/src/Plugin/jsonapi/FieldEnhancer/SingleNestedEnhancer.php @@ -0,0 +1,123 @@ +<?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 = "nested", + * label = @Translation("Single Nested Property"), + * description = @Translation("Extracts or wraps nested properties from an object.") + * ) + */ +class SingleNestedEnhancer 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 [ + 'path' => 'value', + ]; + } + + /** + * {@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) { + $output = $value; + $configuration = $this->getConfiguration(); + $path = $configuration['path']; + $path_parts = explode('.', $path); + // Start drilling down until there are no more path parts. + while ($output && ($path_part = array_shift($path_parts))) { + $output = empty($output[$path_part]) + ? NULL + : $output[$path_part]; + } + return $output; + } + + /** + * {@inheritdoc} + */ + public function prepareForInput($value) { + $input = $value; + $configuration = $this->getConfiguration(); + $path = $configuration['path']; + $path_parts = explode('.', $path); + // Start wrapping up until there are no more path parts. + while ($path_part = array_pop($path_parts)) { + $input = [$path_part => $input]; + } + return $input; + } + + /** + * {@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 [ + 'path' => [ + '#type' => 'textfield', + '#title' => $this->t('Path'), + '#description' => $this->t('A dot separated path to extract the sub-property.'), + '#default_value' => $settings['path'], + ], + ]; + } + +} |