Issue #2017139 by amateescu, swentel: Convert field type to typed data plugin for datetime module .
parent
e47a081920
commit
362e5f82c4
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Install, update and uninstall functions for the Datetime module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_field_schema().
|
||||
*/
|
||||
function datetime_field_schema($field) {
|
||||
$db_columns = array();
|
||||
$db_columns['value'] = array(
|
||||
'description' => 'The date value',
|
||||
'type' => 'varchar',
|
||||
'length' => 20,
|
||||
'not null' => FALSE,
|
||||
);
|
||||
$indexes = array(
|
||||
'value' => array('value'),
|
||||
);
|
||||
return array('columns' => $db_columns, 'indexes' => $indexes);
|
||||
}
|
|
@ -90,74 +90,6 @@ function datetime_theme() {
|
|||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_is_empty().
|
||||
*/
|
||||
function datetime_field_is_empty($item, $field_type) {
|
||||
if (empty($item['value'])) {
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_info().
|
||||
*/
|
||||
function datetime_field_info() {
|
||||
return array(
|
||||
'datetime' => array(
|
||||
'label' => 'Date',
|
||||
'description' => t('Create and store date values.'),
|
||||
'settings' => array(
|
||||
'datetime_type' => 'datetime',
|
||||
),
|
||||
'instance_settings' => array(
|
||||
'default_value' => 'now',
|
||||
),
|
||||
'default_widget' => 'datetime_default',
|
||||
'default_formatter' => 'datetime_default',
|
||||
'class' => '\Drupal\datetime\Type\DateTimeItem',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_settings_form().
|
||||
*/
|
||||
function datetime_field_settings_form($field, $instance) {
|
||||
$settings = $field['settings'];
|
||||
|
||||
$form['datetime_type'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date type'),
|
||||
'#description' => t('Choose the type of date to create.'),
|
||||
'#default_value' => $settings['datetime_type'],
|
||||
'#options' => array(
|
||||
'datetime' => t('Date and time'),
|
||||
'date' => t('Date only'),
|
||||
),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_instance_settings_form().
|
||||
*/
|
||||
function datetime_field_instance_settings_form($field, $instance) {
|
||||
$settings = $instance['settings'];
|
||||
|
||||
$form['default_value'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Default date'),
|
||||
'#description' => t('Set a default value for this date.'),
|
||||
'#default_value' => $settings['default_value'],
|
||||
'#options' => array('blank' => t('No default value'), 'now' => t('The current date')),
|
||||
'#weight' => 1,
|
||||
);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validation callback for the datetime widget element.
|
||||
*
|
||||
|
@ -234,30 +166,6 @@ function datetime_datelist_widget_validate(&$element, &$form_state) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_load().
|
||||
*
|
||||
* The function generates a Date object for each field early so that it is
|
||||
* cached in the field cache. This avoids the need to generate the object later.
|
||||
* The date will be retrieved in UTC, the local timezone adjustment must be made
|
||||
* in real time, based on the preferences of the site and user.
|
||||
*/
|
||||
function datetime_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
|
||||
foreach ($entities as $id => $entity) {
|
||||
foreach ($items[$id] as $delta => $item) {
|
||||
$items[$id][$delta]['date'] = NULL;
|
||||
$value = isset($item['value']) ? $item['value'] : NULL;
|
||||
if (!empty($value)) {
|
||||
$storage_format = $field['settings']['datetime_type'] == 'date' ? DATETIME_DATE_STORAGE_FORMAT: DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$date = new DrupalDateTime($value, DATETIME_STORAGE_TIMEZONE, $storage_format);
|
||||
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
|
||||
$items[$id][$delta]['date'] = $date;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a default value for an empty date field.
|
||||
*
|
||||
|
|
|
@ -0,0 +1,140 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\datetime\Plugin\field\field_type\DateTimeItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\datetime\Plugin\field\field_type;
|
||||
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\Core\Datetime\DrupalDateTime;
|
||||
use Drupal\Core\Entity\Annotation\FieldType;
|
||||
use Drupal\Core\Entity\Field\PrepareCacheInterface;
|
||||
use Drupal\field\Plugin\Core\Entity\Field;
|
||||
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'datetime' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "datetime",
|
||||
* label = @Translation("Date"),
|
||||
* description = @Translation("Create and store date values."),
|
||||
* settings = {
|
||||
* "datetime_type" = "datetime"
|
||||
* },
|
||||
* instance_settings = {
|
||||
* "default_value" = "now"
|
||||
* },
|
||||
* default_widget = "datetime_default",
|
||||
* default_formatter = "datetime_default"
|
||||
* )
|
||||
*/
|
||||
class DateTimeItem extends ConfigFieldItemBase implements PrepareCacheInterface {
|
||||
|
||||
/**
|
||||
* Field definitions of the contained properties.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $propertyDefinitions;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getPropertyDefinitions() {
|
||||
if (!isset(static::$propertyDefinitions)) {
|
||||
static::$propertyDefinitions['value'] = array(
|
||||
'type' => 'datetime_iso8601',
|
||||
'label' => t('Date value'),
|
||||
);
|
||||
}
|
||||
|
||||
return static::$propertyDefinitions;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(Field $field) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'value' => array(
|
||||
'description' => 'The date value.',
|
||||
'type' => 'varchar',
|
||||
'length' => 20,
|
||||
'not null' => FALSE,
|
||||
),
|
||||
),
|
||||
'indexes' => array(
|
||||
'value' => array('value'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, array &$form_state) {
|
||||
$element = array();
|
||||
|
||||
$element['datetime_type'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Date type'),
|
||||
'#description' => t('Choose the type of date to create.'),
|
||||
'#default_value' => $this->getFieldSetting('datetime_type'),
|
||||
'#options' => array(
|
||||
'datetime' => t('Date and time'),
|
||||
'date' => t('Date only'),
|
||||
),
|
||||
);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function instanceSettingsForm(array $form, array &$form_state) {
|
||||
$element = array();
|
||||
|
||||
$element['default_value'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Default date'),
|
||||
'#description' => t('Set a default value for this date.'),
|
||||
'#default_value' => $this->getFieldSetting('default_value'),
|
||||
'#options' => array('blank' => t('No default value'), 'now' => t('The current date')),
|
||||
'#weight' => 1,
|
||||
);
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function prepareCache() {
|
||||
// The function generates a Date object for each field early so that it is
|
||||
// cached in the field cache. This avoids the need to generate the object
|
||||
// later. The date will be retrieved in UTC, the local timezone adjustment
|
||||
// must be made in real time, based on the preferences of the site and user.
|
||||
$value = $this->get('value')->getValue();
|
||||
if (!empty($value)) {
|
||||
$storage_format = $this->getFieldSetting('datetime_type') == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
|
||||
$date = new DrupalDateTime($value, DATETIME_STORAGE_TIMEZONE, $storage_format);
|
||||
if ($date instanceOf DrupalDateTime && !$date->hasErrors()) {
|
||||
$this->set('date', $date);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isEmpty() {
|
||||
$value = $this->get('value')->getValue();
|
||||
return $value === NULL || $value === '';
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains Drupal\datetime\Type\DateTimeItem.
|
||||
*/
|
||||
|
||||
namespace Drupal\datetime\Type;
|
||||
|
||||
use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem;
|
||||
|
||||
/**
|
||||
* Defines the 'datetime' entity field item.
|
||||
*/
|
||||
class DateTimeItem extends LegacyConfigFieldItem {
|
||||
|
||||
/**
|
||||
* Field definitions of the contained properties.
|
||||
*
|
||||
* @see self::getPropertyDefinitions()
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
static $propertyDefinitions;
|
||||
|
||||
/**
|
||||
* Implements ComplexDataInterface::getPropertyDefinitions().
|
||||
*/
|
||||
public function getPropertyDefinitions() {
|
||||
|
||||
if (!isset(self::$propertyDefinitions)) {
|
||||
self::$propertyDefinitions['value'] = array(
|
||||
'type' => 'datetime_iso8601',
|
||||
'label' => t('Date value'),
|
||||
);
|
||||
}
|
||||
return self::$propertyDefinitions;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue