public function OverrideUserMailRequiredValidator::validate in Administer Users by Role 8.3
Same name and namespace in other branches
- 8.2 src/Constraint/OverrideUserMailRequiredValidator.php \Drupal\administerusersbyrole\Constraint\OverrideUserMailRequiredValidator::validate()
Overrides UserMailRequiredValidator::validate
File
- src/
Constraint/ OverrideUserMailRequiredValidator.php, line 20
Class
- OverrideUserMailRequiredValidator
- Checks if the user's email address is provided if required.
Namespace
Drupal\administerusersbyrole\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::entityTypeManager()
->getStorage('user')
->loadUnchanged($account
->id());
$existing_value = $account_unchanged
->getEmail();
}
$has_permission = \Drupal::currentUser()
->hasPermission('administer users') || \Drupal::currentUser()
->hasPermission('allow empty user mail');
$required = !(!$existing_value && $has_permission);
if ($required && (!isset($items) || $items
->isEmpty())) {
$this->context
->addViolation($constraint->message, [
'@name' => $account
->getFieldDefinition('mail')
->getLabel(),
]);
}
}