3 namespace Drupal\telephone\Plugin\Field\FieldType;
5 use Drupal\Core\Field\FieldDefinitionInterface;
6 use Drupal\Core\Field\FieldItemBase;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\TypedData\DataDefinition;
11 * Plugin implementation of the 'telephone' field type.
15 * label = @Translation("Telephone number"),
16 * description = @Translation("This field stores a telephone number in the database."),
17 * category = @Translation("Number"),
18 * default_widget = "telephone_default",
19 * default_formatter = "basic_string"
22 class TelephoneItem extends FieldItemBase {
27 public static function schema(FieldStorageDefinitionInterface $field_definition) {
41 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
42 $properties['value'] = DataDefinition::create('string')
43 ->setLabel(t('Telephone number'))
52 public function isEmpty() {
53 $value = $this->get('value')->getValue();
54 return $value === NULL || $value === '';
60 public function getConstraints() {
61 $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
62 $constraints = parent::getConstraints();
65 $constraints[] = $constraint_manager->create('ComplexData', [
69 'maxMessage' => t('%name: the telephone number may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => $max_length]),
80 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
81 $values['value'] = rand(pow(10, 8), pow(10, 9) - 1);