public function OgAddMultipleRolesForm::submitForm in Organic groups 8
Form submission 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 OgChangeMultipleRolesFormBase::submitForm
File
- src/
Form/ OgAddMultipleRolesForm.php, line 52
Class
- OgAddMultipleRolesForm
- Provides a form to add multiple OG roles to a membership.
Namespace
Drupal\og\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$role_ids = array_keys($form_state
->getValue('roles'));
/** @var \Drupal\og\OgRoleInterface[] $roles */
$roles = OgRole::loadMultiple($role_ids);
foreach ($this
->getMemberships() as $membership) {
$changed = FALSE;
foreach ($roles as $role) {
$group = $membership
->getGroup();
if ($group
->getEntityTypeId() === $role
->getGroupType() && $group
->bundle() === $role
->getGroupBundle()) {
// Only add the role to the membership if it is valid and doesn't
// exist yet.
if ($membership
->isRoleValid($role) && !$membership
->hasRole($role
->id())) {
$changed = TRUE;
$membership
->addRole($role);
}
}
}
// Only save the membership if it has actually changed.
if ($changed) {
$membership
->save();
}
}
}