Skip to content
Snippets Groups Projects
Select Git revision
  • 9.0.x
  • 11.x default protected
  • 10.5.x protected
  • 11.2.x protected
  • 10.6.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
40 results

EmailItem.php

Blame
  • Alex Pott's avatar
    Issue #2572613 by Malevi4, idebr, pfrenssen, Mile23, RajeevK: Fix...
    Alex Pott authored
    Issue #2572613 by Malevi4, idebr, pfrenssen, Mile23, RajeevK: Fix 'Drupal.Array.Array.CommaLastItem' coding standard
    be6d515c
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    EmailItem.php 2.10 KiB
    <?php
    
    namespace Drupal\Core\Field\Plugin\Field\FieldType;
    
    use Drupal\Component\Utility\Random;
    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;
    
    /**
     * Defines the 'email' field type.
     *
     * @FieldType(
     *   id = "email",
     *   label = @Translation("Email"),
     *   description = @Translation("An entity field containing an email value."),
     *   default_widget = "email_default",
     *   default_formatter = "basic_string"
     * )
     */
    class EmailItem extends FieldItemBase {
    
      /**
       * {@inheritdoc}
       */
      public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
        $properties['value'] = DataDefinition::create('email')
          ->setLabel(t('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' => 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 === '';
      }
    
    }