You are here

class UserMailRequired in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php \Drupal\user\Plugin\Validation\Constraint\UserMailRequired

Checks if the user's email address is provided if required.

The user mail field is NOT required if account originally had no mail set and the user performing the edit has 'administer users' permission. This allows users without email address to be edited and deleted.

Plugin annotation


@Constraint(
  id = "UserMailRequired",
  label = @Translation("User email required", context = "Validation")
)

Hierarchy

Expanded class hierarchy of UserMailRequired

File

core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php, line 26
Contains \Drupal\user\Plugin\Validation\Constraint\UserMailRequired.

Namespace

Drupal\user\Plugin\Validation\Constraint
View source
class UserMailRequired extends Constraint implements ConstraintValidatorInterface {

  /**
   * Violation message. Use the same message as FormValidator.
   *
   * Note that the name argument is not sanitized so that translators only have
   * one string to translate. The name is sanitized in self::validate().
   *
   * @var string
   */
  public $message = '@name field is required.';

  /**
   * @var \Symfony\Component\Validator\ExecutionContextInterface
   */
  protected $context;

  /**
   * {@inheritDoc}
   */
  public function initialize(ExecutionContextInterface $context) {
    $this->context = $context;
  }

  /**
   * {@inheritdoc}
   */
  public function validatedBy() {
    return get_class($this);
  }

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {

    /** @var \Drupal\Core\Field\FieldItemListInterface $items */

    /** @var \Drupal\user\UserInterface $account */
    $account = $items
      ->getEntity();
    $existing_value = NULL;
    if ($account
      ->id()) {
      $account_unchanged = \Drupal::entityManager()
        ->getStorage('user')
        ->loadUnchanged($account
        ->id());
      $existing_value = $account_unchanged
        ->getEmail();
    }
    $required = !(!$existing_value && \Drupal::currentUser()
      ->hasPermission('administer users'));
    if ($required && (!isset($items) || $items
      ->isEmpty())) {
      $this->context
        ->addViolation($this->message, [
        '@name' => $account
          ->getFieldDefinition('mail')
          ->getLabel(),
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Constraint::$errorNames protected static property Maps error codes to the names of their constants. 16
Constraint::$payload public property Domain-specific data attached to a constraint.
Constraint::addImplicitGroupName public function Adds the given group if this constraint is in the Default group. 1
Constraint::CLASS_CONSTRAINT constant Marks a constraint that can be put onto classes.
Constraint::DEFAULT_GROUP constant The name of the group given to all constraints with no explicit group.
Constraint::getDefaultOption public function Returns the name of the default option. 18
Constraint::getErrorName public static function Returns the name of the given error code.
Constraint::getRequiredOptions public function Returns the name of the required options. 11
Constraint::getTargets public function Returns whether the constraint can be put onto classes, properties or both. 11
Constraint::PROPERTY_CONSTRAINT constant Marks a constraint that can be put onto properties.
Constraint::__construct public function Initializes the constraint with options. 11
Constraint::__get public function Returns the value of a lazily initialized option. 1
Constraint::__set public function Sets the value of a lazily initialized option. 1
Constraint::__sleep public function Optimizes the serialized value to minimize storage space.
UserMailRequired::$context protected property
UserMailRequired::$message public property Violation message. Use the same message as FormValidator.
UserMailRequired::initialize public function Initializes the constraint validator. Overrides ConstraintValidatorInterface::initialize
UserMailRequired::validate public function Checks if the passed value is valid. Overrides ConstraintValidatorInterface::validate
UserMailRequired::validatedBy public function Returns the name of the class that validates this constraint. Overrides Constraint::validatedBy