UserMailRequiredValidator.php in Drupal 10
File
core/modules/user/src/Plugin/Validation/Constraint/UserMailRequiredValidator.php
View source
<?php
namespace Drupal\user\Plugin\Validation\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Constraint;
class UserMailRequiredValidator extends ConstraintValidator {
public function validate($items, Constraint $constraint) {
$account = $items
->getEntity();
if (!isset($account)) {
return;
}
$existing_value = NULL;
if (!$account
->isNew()) {
$account_unchanged = \Drupal::entityTypeManager()
->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($constraint->message, [
'@name' => $account
->getFieldDefinition('mail')
->getLabel(),
]);
}
}
}