You are here

public function OgChangeMultipleRolesFormBase::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 FormInterface::submitForm

2 methods override OgChangeMultipleRolesFormBase::submitForm()
OgAddMultipleRolesForm::submitForm in src/Form/OgAddMultipleRolesForm.php
Form submission handler.
OgRemoveMultipleRolesForm::submitForm in src/Form/OgRemoveMultipleRolesForm.php
Form submission handler.

File

src/Form/OgChangeMultipleRolesFormBase.php, line 102

Class

OgChangeMultipleRolesFormBase
Base class for forms that act on multiple roles.

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();
    }
  }
}