You are here

public function UserSectionStorage::removeUser in Workbench Access 8

Removes a set of sections to a user.

Parameters

\Drupal\workbench_access\Entity\AccessSchemeInterface $scheme: Access scheme.

\Drupal\Core\Session\AccountInterface $account: User to remove from the section.

array $sections: An array of section ids to assign to this user.

Return value

\Drupal\Core\Session\AccountInterface The saved user entity.

Overrides UserSectionStorageInterface::removeUser

File

src/UserSectionStorage.php, line 136

Class

UserSectionStorage
Defines a class for storing and retrieving sections assigned to a user.

Namespace

Drupal\workbench_access

Code

public function removeUser(AccessSchemeInterface $scheme, AccountInterface $account, array $sections = []) {
  foreach ($sections as $id) {
    $new_values = [];
    if ($section_association = $this
      ->sectionStorage()
      ->loadSection($scheme
      ->id(), $id)) {
      if ($values = $section_association
        ->getCurrentUserIds()) {
        foreach ($values as $delta => $value) {
          if ($value != $account
            ->id()) {
            $new_values[] = $value;
          }
        }
        $section_association
          ->set('user_id', array_unique($new_values));
      }
      $section_association
        ->save();
    }
  }
  $this
    ->resetCache($scheme, $account
    ->id());

  // Return the user object.
  return $this
    ->userStorage()
    ->load($account
    ->id());
}