You are here

class OverrideUserMailRequiredValidator in Administer Users by Role 8.3

Same name and namespace in other branches
  1. 8.2 src/Constraint/OverrideUserMailRequiredValidator.php \Drupal\administerusersbyrole\Constraint\OverrideUserMailRequiredValidator

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 permission to set empty user mail. This allows users without email address to be edited and deleted.

Hierarchy

Expanded class hierarchy of OverrideUserMailRequiredValidator

File

src/Constraint/OverrideUserMailRequiredValidator.php, line 15

Namespace

Drupal\administerusersbyrole\Constraint
View source
class OverrideUserMailRequiredValidator extends UserMailRequiredValidator {

  /**
   * {@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::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(),
      ]);
    }
  }

}

Members