You are here

public function ContactEmailUniqueValidator::validate in RedHen CRM 8

File

modules/redhen_contact/src/Plugin/Validation/Constraint/ContactEmailUniqueValidator.php, line 16

Class

ContactEmailUniqueValidator
Validates that a field is unique for the given entity type.

Namespace

Drupal\redhen_contact\Plugin\Validation\Constraint

Code

public function validate($items, Constraint $constraint) {

  // Get Contact configuration.
  $config = \Drupal::config('redhen_contact.settings');

  // If mirroring Contacts to Drupal Users or config specifies unique email,
  // ensure email is unique.
  if ($config
    ->get('connect_users') || $config
    ->get('unique_email')) {
    $email = $this->context
      ->getValue()->value;

    // $id must not be null or the entityQuery will never return results.
    $id = $this->context
      ->getValue()
      ->getParent()
      ->getValue()->id->value === NULL ? 0 : $this->context
      ->getValue()
      ->getParent()
      ->getValue()->id->value;

    // Query to find out if email is taken.
    $email_taken = (bool) \Drupal::entityQuery('redhen_contact')
      ->condition('email', $email)
      ->condition('id', $id, '!=')
      ->execute();

    // Validation fails if email is already taken, succeeds otherwise.
    if ($email_taken) {
      $this->context
        ->addViolation($constraint->message, [
        '%value' => $email,
      ]);
    }
  }
}