EmailItem.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php
  3. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EmailItem.php

View source
<?php

namespace Drupal\Core\Field\Plugin\Field\FieldType;

use Drupal\Component\Utility\Random;
use Drupal\Core\Field\Attribute\FieldType;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Render\Element\Email;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines the 'email' field type.
 */
class EmailItem extends FieldItemBase {
    
    /**
     * {@inheritdoc}
     */
    public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
        $properties['value'] = DataDefinition::create('email')->setLabel(new TranslatableMarkup('Email'))
            ->setRequired(TRUE);
        return $properties;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function schema(FieldStorageDefinitionInterface $field_definition) {
        return [
            'columns' => [
                'value' => [
                    'type' => 'varchar',
                    'length' => Email::EMAIL_MAX_LENGTH,
                ],
            ],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConstraints() {
        $constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
        $constraints = parent::getConstraints();
        $constraints[] = $constraint_manager->create('ComplexData', [
            'value' => [
                'Length' => [
                    'max' => Email::EMAIL_MAX_LENGTH,
                    'maxMessage' => $this->t('%name: the email address can not be longer than @max characters.', [
                        '%name' => $this->getFieldDefinition()
                            ->getLabel(),
                        '@max' => Email::EMAIL_MAX_LENGTH,
                    ]),
                ],
            ],
        ]);
        return $constraints;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
        $random = new Random();
        $values['value'] = $random->name() . '@example.com';
        return $values;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isEmpty() {
        return $this->value === NULL || $this->value === '';
    }

}

Classes

Title Deprecated Summary
EmailItem Defines the 'email' field type.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.