You are here

public function RoleSectionStorage::addRole in Workbench Access 8

Adds a set of sections to a role.

Parameters

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

string $role_id: A role id.

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

Overrides RoleSectionStorageInterface::addRole

File

src/RoleSectionStorage.php, line 61

Class

RoleSectionStorage
Defines a role-section storage that uses the State API.

Namespace

Drupal\workbench_access

Code

public function addRole(AccessSchemeInterface $scheme, $role_id, array $sections = []) {
  foreach ($sections as $id) {
    if ($section_association = $this
      ->sectionStorage()
      ->loadSection($scheme
      ->id(), $id)) {
      if ($new_values = $section_association
        ->getCurrentRoleIds()) {
        $new_values[] = $role_id;
        $section_association
          ->set('role_id', array_unique($new_values));
      }
      else {
        $section_association
          ->set('role_id', [
          $role_id,
        ]);
      }
      $section_association
        ->setNewRevision();
    }
    else {
      $values = [
        'access_scheme' => $scheme
          ->id(),
        'section_id' => $id,
        'role_id' => [
          $role_id,
        ],
      ];
      $section_association = $this
        ->sectionStorage()
        ->create($values);
    }
    $section_association
      ->save();
    \Drupal::service('workbench_access.user_section_storage')
      ->resetCache($scheme);
  }
}