You are here

public function EnrollInviteEmailForm::validateForm in Open Social 8.9

Same name and namespace in other branches
  1. 10.3.x modules/social_features/social_event/modules/social_event_invite/src/Form/EnrollInviteEmailForm.php \Drupal\social_event_invite\Form\EnrollInviteEmailForm::validateForm()
  2. 10.0.x modules/social_features/social_event/modules/social_event_invite/src/Form/EnrollInviteEmailForm.php \Drupal\social_event_invite\Form\EnrollInviteEmailForm::validateForm()
  3. 10.1.x modules/social_features/social_event/modules/social_event_invite/src/Form/EnrollInviteEmailForm.php \Drupal\social_event_invite\Form\EnrollInviteEmailForm::validateForm()
  4. 10.2.x modules/social_features/social_event/modules/social_event_invite/src/Form/EnrollInviteEmailForm.php \Drupal\social_event_invite\Form\EnrollInviteEmailForm::validateForm()

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

modules/social_features/social_event/modules/social_event_invite/src/Form/EnrollInviteEmailForm.php, line 188

Class

EnrollInviteEmailForm
Class EnrollInviteForm.

Namespace

Drupal\social_event_invite\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $nid = $form_state
    ->getValue('event');

  // Check if the user is already enrolled.
  foreach ($form_state
    ->getValue('users_fieldset')['user'] as $user) {

    // Check if the user is a filled in email.
    $email = $this
      ->extractEmailsFrom($user);
    if ($email) {
      $conditions = [
        'field_email' => $email,
        'field_event' => $nid,
      ];
    }
    else {
      $conditions = [
        'field_account' => $user,
        'field_event' => $nid,
      ];
    }
    $enrollments = $this->entityStorage
      ->loadByProperties($conditions);
    if (!empty($enrollments)) {

      /** @var \Drupal\social_event\Entity\EventEnrollment $enrollment */
      $enrollment = end($enrollments);

      // Of course, only delete the previous invite if it was declined
      // or if it was invalid or expired.
      $status_checks = [
        EventEnrollmentInterface::REQUEST_OR_INVITE_DECLINED,
        EventEnrollmentInterface::INVITE_INVALID_OR_EXPIRED,
      ];
      if (in_array($enrollment->field_request_or_invite_status->value, $status_checks)) {
        $enrollment
          ->delete();
        unset($enrollments[$enrollment
          ->id()]);
      }
    }

    // If enrollments can be found this user is already invited or joined.
    if (!empty($enrollments)) {

      // If the user is already enrolled, don't enroll them again.
      $form_state
        ->unsetValue([
        'users_fieldset',
        'user',
        $user,
      ]);
    }
  }
}