You are here

public function OgRemoveMultipleRolesForm::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/OgRemoveMultipleRolesForm.php, line 51

Class

OgRemoveMultipleRolesForm
Provides a form to remove multiple OG roles from a membership.

Namespace

Drupal\og\Form

Code

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()) {
        if ($membership
          ->hasRole($role
          ->id())) {
          $changed = TRUE;
          $membership
            ->revokeRole($role);
        }
      }
    }

    // Only save the membership if it has actually changed.
    if ($changed) {
      $membership
        ->save();
    }
  }
}