Newer
Older
<?php
/**
* @file
* Contains \Drupal\Core\Field\Plugin\Field\FieldType\EmailItem.
*/
Alex Pott
committed
namespace Drupal\Core\Field\Plugin\Field\FieldType;
use Drupal\Component\Utility\Random;
use Drupal\Core\Field\FieldDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FieldStorageDefinitionInterface;
Alex Pott
committed
use Drupal\Core\Field\FieldItemBase;
Angie Byron
committed
use Drupal\Core\Render\Element\Email;
use Drupal\Core\TypedData\DataDefinition;
/**
* Defines the 'email' field type.
Alex Pott
committed
*
Alex Pott
committed
* @FieldType(
* id = "email",
* label = @Translation("Email"),
* description = @Translation("An entity field containing an email value."),
* default_widget = "email_default",
catch
committed
* default_formatter = "basic_string"
Alex Pott
committed
* )
*/
Alex Pott
committed
class EmailItem extends FieldItemBase {
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['value'] = DataDefinition::create('email')
->setLabel(t('E-mail'))
->setRequired(TRUE);
return $properties;
Alex Pott
committed
/**
* {@inheritdoc}
*/
Alex Pott
committed
public static function schema(FieldStorageDefinitionInterface $field_definition) {
Alex Pott
committed
return array(
'columns' => array(
'value' => array(
'type' => 'varchar',
Angie Byron
committed
'length' => Email::EMAIL_MAX_LENGTH,
Alex Pott
committed
),
),
);
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
$constraints = parent::getConstraints();
$constraints[] = $constraint_manager->create('ComplexData', array(
'value' => array(
'Length' => array(
Angie Byron
committed
'max' => Email::EMAIL_MAX_LENGTH,
'maxMessage' => t('%name: the email address can not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => Email::EMAIL_MAX_LENGTH)),
Alex Pott
committed
)
),
));
return $constraints;
}
Alex Pott
committed
/**
* {@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 === '';
}
Alex Pott
committed