Unverified Commit 7321bedd authored by szeidler's avatar szeidler Committed by Mateu Aguiló Bosch

feat(DateTime): Allow DateTime enhancement for non-timestamp fields (#2921612 by szeidler)

parent 6ba06da8
<?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'],
],
];
}
}
......@@ -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'],
],
];
}
}
<?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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment