TimeRangeType.php in Field Time 1.0.x
File
src/Plugin/Field/FieldType/TimeRangeType.php
View source
<?php
namespace Drupal\field_time\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\TypedData\DataDefinition;
class TimeRangeType extends FieldItemBase {
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['from'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('Start time'))
->setDescription(new TranslatableMarkup('Format HH:MM:SS'))
->setSetting('maxlength', 8)
->setRequired(TRUE);
$properties['to'] = DataDefinition::create('string')
->setLabel(new TranslatableMarkup('End time'))
->setDescription(new TranslatableMarkup('Format HH:MM:SS'))
->setSetting('maxlength', 8)
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'from' => [
'type' => 'varchar_ascii',
'length' => 8,
'not null' => FALSE,
],
'to' => [
'type' => 'varchar_ascii',
'length' => 8,
'not null' => FALSE,
],
],
'indexes' => [
'value' => [
'from',
'to',
],
],
];
return $schema;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['from'] = '10:55:00';
$values['to'] = '11:55:00';
return $values;
}
public function isEmpty() {
$from = $this
->get('from')
->getValue();
$to = $this
->get('to')
->getValue();
return trim($from) === '' || trim($to) === '';
}
}