TimeZoneItem.php in Time Zone Field 8
File
src/Plugin/Field/FieldType/TimeZoneItem.php
View source
<?php
namespace Drupal\tzfield\Plugin\Field\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;
class TimeZoneItem extends FieldItemBase {
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'value' => [
'type' => 'varchar',
'length' => 50,
],
],
];
}
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('string')
->setLabel(t('Time zone'))
->setRequired(TRUE);
return $properties;
}
public function isEmpty() {
$value = $this
->get('value')
->getValue();
return $value === NULL || $value === '';
}
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()
->getValidationConstraintManager();
$constraints = parent::getConstraints();
$max_length = 50;
$constraints[] = $constraint_manager
->create('ComplexData', [
'value' => [
'Length' => [
'max' => $max_length,
'maxMessage' => $this
->t('%name: The time zone name may not be longer than @max characters.', [
'%name' => $this
->getFieldDefinition()
->getLabel(),
'@max' => $max_length,
]),
],
],
]);
return $constraints;
}
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
$values['value'] = array_rand(system_time_zones());
return $values;
}
}
Classes
Name |
Description |
TimeZoneItem |
Plugin implementation of the time zone field type. |