You are here

function workbench_access_post_update_section_role_association in Workbench Access 8

Transform existing role data to new storage.

File

./workbench_access.post_update.php, line 120
Contains post update hooks.

Code

function workbench_access_post_update_section_role_association(&$sandbox) {
  $schemes = \Drupal::entityTypeManager()
    ->getStorage('access_scheme')
    ->loadMultiple();
  $storage = \Drupal::service('workbench_access.role_section_storage');
  $state = \Drupal::state();
  foreach ($schemes as $scheme) {
    foreach (\Drupal::entityTypeManager()
      ->getStorage('user_role')
      ->loadMultiple() as $rid => $role) {
      $prefix = 'workbench_access_roles_';
      $potential_ids = [
        $prefix . $rid,
        $prefix . 'default__' . $rid,
      ];
      foreach ($potential_ids as $key) {
        if ($existing = $state
          ->get($key, FALSE)) {

          // Save the new roles.
          $storage
            ->addRole($scheme, $rid, array_values($existing));

          // Delete the old storage.
          $state
            ->delete($key);
        }
      }
    }
  }
}