ca30ede268112ff41b2d766750c6d0169f697796
[yaffs-website] / web / core / lib / Drupal / Core / Field / Plugin / Field / FieldType / EmailItem.php
1 <?php
2
3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
4
5 use Drupal\Component\Utility\Random;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\Core\Field\FieldStorageDefinitionInterface;
8 use Drupal\Core\Field\FieldItemBase;
9 use Drupal\Core\Render\Element\Email;
10 use Drupal\Core\TypedData\DataDefinition;
11
12 /**
13  * Defines the 'email' field type.
14  *
15  * @FieldType(
16  *   id = "email",
17  *   label = @Translation("Email"),
18  *   description = @Translation("An entity field containing an email value."),
19  *   default_widget = "email_default",
20  *   default_formatter = "basic_string"
21  * )
22  */
23 class EmailItem extends FieldItemBase {
24
25   /**
26    * {@inheritdoc}
27    */
28   public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
29     $properties['value'] = DataDefinition::create('email')
30       ->setLabel(t('Email'))
31       ->setRequired(TRUE);
32
33     return $properties;
34   }
35
36   /**
37    * {@inheritdoc}
38    */
39   public static function schema(FieldStorageDefinitionInterface $field_definition) {
40     return [
41       'columns' => [
42         'value' => [
43           'type' => 'varchar',
44           'length' => Email::EMAIL_MAX_LENGTH,
45         ],
46       ],
47     ];
48   }
49
50   /**
51    * {@inheritdoc}
52    */
53   public function getConstraints() {
54     $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
55     $constraints = parent::getConstraints();
56
57     $constraints[] = $constraint_manager->create('ComplexData', [
58       'value' => [
59         'Length' => [
60           'max' => Email::EMAIL_MAX_LENGTH,
61           'maxMessage' => t('%name: the email address can not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH]),
62         ]
63       ],
64     ]);
65
66     return $constraints;
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
73     $random = new Random();
74     $values['value'] = $random->name() . '@example.com';
75     return $values;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function isEmpty() {
82     return $this->value === NULL || $this->value === '';
83   }
84
85 }