3 namespace Drupal\Core\Field\Plugin\Field\FieldType;
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;
13 * Defines the 'email' field type.
17 * label = @Translation("Email"),
18 * description = @Translation("An entity field containing an email value."),
19 * default_widget = "email_default",
20 * default_formatter = "basic_string"
23 class EmailItem extends FieldItemBase {
28 public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
29 $properties['value'] = DataDefinition::create('email')
30 ->setLabel(t('Email'))
39 public static function schema(FieldStorageDefinitionInterface $field_definition) {
44 'length' => Email::EMAIL_MAX_LENGTH,
53 public function getConstraints() {
54 $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
55 $constraints = parent::getConstraints();
57 $constraints[] = $constraint_manager->create('ComplexData', [
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]),
72 public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
73 $random = new Random();
74 $values['value'] = $random->name() . '@example.com';
81 public function isEmpty() {
82 return $this->value === NULL || $this->value === '';