public function UserMailRequired::validate in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/user/src/Plugin/Validation/Constraint/UserMailRequired.php \Drupal\user\Plugin\Validation\Constraint\UserMailRequired::validate()
Checks if the passed value is valid.
Parameters
mixed $value The value that should be validated:
Constraint $constraint The constraint for the validation:
Overrides ConstraintValidatorInterface::validate
File
- core/
modules/ user/ src/ Plugin/ Validation/ Constraint/ UserMailRequired.php, line 60 - Contains \Drupal\user\Plugin\Validation\Constraint\UserMailRequired.
Class
- UserMailRequired
- Checks if the user's email address is provided if required.
Namespace
Drupal\user\Plugin\Validation\ConstraintCode
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(),
]);
}
}