You are here

function PetPreviewForm::pet_validate_recipients in Previewable email templates 8.4

Same name and namespace in other branches
  1. 8 src/Form/PetPreviewForm.php \Drupal\pet\Form\PetPreviewForm::pet_validate_recipients()

Validate existence of a non-empty recipient list free of email errors.

1 call to PetPreviewForm::pet_validate_recipients()
PetPreviewForm::validateForm in src/Form/PetPreviewForm.php
Form validation handler.

File

src/Form/PetPreviewForm.php, line 316

Class

PetPreviewForm

Namespace

Drupal\pet\Form

Code

function pet_validate_recipients(FormStateInterface $form_state, &$recipients) {
  $errors = [];
  $recipients = [];
  if ($form_state
    ->getValue('recipient_callback')) {

    // Get recipients from callback
    $mails = pet_callback_recipients($form_state);
    if (!is_array($mails)) {
      $errors[] = t('There is no recipient callback defined for this template or it is not returning an array.');
      return $errors;
    }
  }
  else {

    // Get recipients from form field
    $mails = pet_parse_mails($form_state
      ->getValue('recipients'));
  }

  // Validate and build recipient array with uid on the fly
  foreach ($mails as $mail) {
    if (!\Drupal::service('email.validator')
      ->isValid($mail)) {
      $errors[] = t('Invalid email address found: %mail.', [
        '%mail' => $mail,
      ]);
    }
    else {
      $recipients[] = [
        'mail' => $mail,
        'uid' => pet_lookup_uid($mail),
      ];
    }
  }

  // Check for no recipients
  if (empty($errors) && count($recipients) < 1) {
    $errors[] = t('There are no recipients for this email.');
  }
  return $errors;
}