class UserMailRequired in Zircon Profile 8.0
Same name and namespace in other branches
- 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
- class \Symfony\Component\Validator\Constraint
- class \Drupal\user\Plugin\Validation\Constraint\UserMailRequired implements ConstraintValidatorInterface
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\ConstraintView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Constraint:: |
protected static | property | Maps error codes to the names of their constants. | 16 |
Constraint:: |
public | property | Domain-specific data attached to a constraint. | |
Constraint:: |
public | function | Adds the given group if this constraint is in the Default group. | 1 |
Constraint:: |
constant | Marks a constraint that can be put onto classes. | ||
Constraint:: |
constant | The name of the group given to all constraints with no explicit group. | ||
Constraint:: |
public | function | Returns the name of the default option. | 18 |
Constraint:: |
public static | function | Returns the name of the given error code. | |
Constraint:: |
public | function | Returns the name of the required options. | 11 |
Constraint:: |
public | function | Returns whether the constraint can be put onto classes, properties or both. | 11 |
Constraint:: |
constant | Marks a constraint that can be put onto properties. | ||
Constraint:: |
public | function | Initializes the constraint with options. | 11 |
Constraint:: |
public | function | Returns the value of a lazily initialized option. | 1 |
Constraint:: |
public | function | Sets the value of a lazily initialized option. | 1 |
Constraint:: |
public | function | Optimizes the serialized value to minimize storage space. | |
UserMailRequired:: |
protected | property | ||
UserMailRequired:: |
public | property | Violation message. Use the same message as FormValidator. | |
UserMailRequired:: |
public | function |
Initializes the constraint validator. Overrides ConstraintValidatorInterface:: |
|
UserMailRequired:: |
public | function |
Checks if the passed value is valid. Overrides ConstraintValidatorInterface:: |
|
UserMailRequired:: |
public | function |
Returns the name of the class that validates this constraint. Overrides Constraint:: |