public function SocialBulkGroupInvitation::validateForm in Open Social 8.9
Same name and namespace in other branches
- 10.3.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
- 10.0.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
- 10.1.x modules/social_features/social_group/modules/social_group_invite/src/Form/SocialBulkGroupInvitation.php \Drupal\social_group_invite\Form\SocialBulkGroupInvitation::validateForm()
- 10.2.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 288
Class
- SocialBulkGroupInvitation
- Class SocialBulkGroupInvitation.
Namespace
Drupal\social_group_invite\FormCode
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 {
// If the invitation has already been send, unset it from the list.
if ($this->groupInvitationLoader
->loadByGroup($this->group, NULL, $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());
}
}
}
}
}