View source
<?php
namespace Drupal\smart_date\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\datetime\DateTimeComputed;
class SmartDateItem extends TimestampItem {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('timestamp')
->setLabel(t('Start timestamp value'))
->setRequired(TRUE);
$properties['start_time'] = DataDefinition::create('any')
->setLabel(t('Computed start date'))
->setDescription(t('The computed start DateTime object.'))
->setComputed(TRUE)
->setClass(DateTimeComputed::class)
->setSetting('date source', 'value');
$properties['end_value'] = DataDefinition::create('timestamp')
->setLabel(t('End timestamp value'))
->setRequired(TRUE);
$properties['end_time'] = DataDefinition::create('any')
->setLabel(t('Computed end date'))
->setDescription(t('The computed end DateTime object.'))
->setComputed(TRUE)
->setClass(DateTimeComputed::class)
->setSetting('date source', 'end_value');
$properties['duration'] = DataDefinition::create('integer')
->setLabel(t('Duration, in minutes'))
->setRequired(FALSE);
$properties['rrule'] = DataDefinition::create('integer')
->setLabel(t('RRule ID'))
->setSetting('unsigned', TRUE)
->setRequired(FALSE);
$properties['rrule_index'] = DataDefinition::create('integer')
->setLabel(t('RRule Index'))
->setSetting('unsigned', TRUE)
->setRequired(FALSE);
$properties['timezone'] = DataDefinition::create('string')
->setLabel(t('Timezone'))
->setDescription(t('The timezone of this date.'))
->setSetting('max_length', 32)
->setRequired(FALSE)
->addConstraint('AllowedValues', array_keys(system_time_zones(TRUE, FALSE)));
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'value' => [
'description' => 'The start time value.',
'type' => 'int',
],
'end_value' => [
'description' => 'The end time value.',
'type' => 'int',
],
'duration' => [
'description' => 'The difference between start and end times, in minutes.',
'type' => 'int',
'size' => 'medium',
],
'rrule' => [
'description' => 'The ID an associated recurrence rule.',
'type' => 'int',
],
'rrule_index' => [
'description' => 'The Index of an associated recurrence rule instance.',
'type' => 'int',
],
'timezone' => [
'description' => 'The preferred timezone.',
'type' => 'varchar',
'length' => 32,
],
],
'indexes' => [
'value' => [
'value',
],
'end_value' => [
'end_value',
],
'rrule' => [
'rrule',
],
'rrule_index' => [
'rrule_index',
],
],
];
return $schema;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$timestamp = \Drupal::time()
->getRequestTime() - mt_rand(0, 86400 * 365);
$duration = 60;
$values['value'] = $timestamp;
$values['end_value'] = $timestamp + $duration * 60;
$values['duration'] = $duration;
$values['timezone'] = 'UTC';
return $values;
}
public function isEmpty() {
$start_value = $this
->get('value')
->getValue();
$end_value = $this
->get('end_value')
->getValue();
return ($start_value === NULL || $start_value === '') && ($end_value === NULL || $end_value === '');
}
public function onChange($property_name, $notify = TRUE) {
if ($property_name == 'value') {
$this->start_time = NULL;
}
elseif ($property_name == 'end_value') {
$this->end_time = NULL;
}
parent::onChange($property_name, $notify);
}
}