You are here

public function SocialBulkGroupInvitation::validateForm in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
  2. 10.3.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
  3. 10.0.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
  4. 10.1.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()

Custom form validation.

We override the source form validation, because we offer invitations to both emails and existing users. Also we unset the people already invited, or part of the group so we don't force users a step back to the buildform.

File

modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php, line 304

Class

SocialBulkGroupInvitation
Class SocialBulkGroupInvitation.

Namespace

Drupal\social_group_invite\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {

  // Loop through all the entries.
  foreach ($form_state
    ->getValue('users_fieldset')['user'] as $user) {
    $email = $this
      ->extractEmailsFrom($user);

    // If the entry is an email go through some different checks then
    // when the user would already be part of the platform.
    if ($email) {

      // Load the user by email.
      $account = user_load_by_mail($email);

      // Check if the email is already part of the platform.
      if ($account instanceof UserInterface) {
        $membership = $this->groupMembershipLoader
          ->load($this->group, $account);

        // User is already part of the group, unset it from the list.
        if (!empty($membership)) {
          $form_state
            ->unsetValue([
            'users_fieldset',
            'user',
            $user,
          ]);
        }
      }
      else {

        /** @var \Drupal\group\Entity\Storage\GroupContentStorageInterface $group_content_storage */
        $group_content_storage = $this->entityTypeManager
          ->getStorage('group_content');

        // If the invitation has already been send, unset it from the list.
        // For some reason groupInvitationLoader service doesn't work
        // properly.
        if (!empty($group_content_storage
          ->loadByGroup($this->group, 'group_invitation', [
          'invitee_mail' => $email,
        ]))) {
          $form_state
            ->unsetValue([
            'users_fieldset',
            'user',
            $user,
          ]);
        }
      }
    }
    else {

      // Load the user by userId.
      $account = User::load($user);
      if ($account instanceof UserInterface) {
        $membership = $this->groupMembershipLoader
          ->load($this->group, $account);

        // User is already part of the group, unset it from the list.
        if (!empty($membership)) {
          $form_state
            ->unsetValue([
            'users_fieldset',
            'user',
            $user,
          ]);
        }
        else {

          // Change the uservalue to his email because the bulk invite for
          // groups can only handle emails.
          $form_state
            ->setValue([
            'users_fieldset',
            'user',
            $user,
          ], $account
            ->getEmail());
        }
      }
    }
  }
}