TimeRangeType.php in Time Field For Drupal 8.x / 9.x 8
File
src/Plugin/Field/FieldType/TimeRangeType.php
View source
<?php
namespace Drupal\time_field\Plugin\Field\FieldType;
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('integer')
->setLabel(new TranslatableMarkup('Seconds passed through midnight'))
->setSetting('unsigned', TRUE)
->setSetting('size', 'small')
->setRequired(TRUE);
$properties['to'] = DataDefinition::create('integer')
->setLabel(new TranslatableMarkup('Seconds passed through midnight'))
->setSetting('unsigned', TRUE)
->setSetting('size', 'small')
->setRequired(TRUE);
return $properties;
}
public static function schema(FieldStorageDefinitionInterface $field_definition) {
$schema = [
'columns' => [
'from' => [
'type' => 'int',
'length' => 6,
],
'to' => [
'type' => 'int',
'length' => 6,
],
],
'indexes' => [
'value' => [
'from',
'to',
],
],
];
return $schema;
}
public function isEmpty() {
$from = $this
->get('from')
->getValue();
$to = $this
->get('to')
->getValue();
return trim($from) === '' || trim($to) === '';
}
}