You are here

public function GroupRoleStorage::createSynchronized in Group 8

Same name and namespace in other branches
  1. 2.0.x src/Entity/Storage/GroupRoleStorage.php \Drupal\group\Entity\Storage\GroupRoleStorage::createSynchronized()

Creates group roles for all user roles.

Parameters

string[] $group_type_ids: (optional) A list of group type IDs to synchronize roles for. Leave empty to synchronize roles for all group types.

string[] $role_ids: (optional) A list of user role IDs to synchronize. Leave empty to synchronize all user roles.

Overrides GroupRoleStorageInterface::createSynchronized

File

src/Entity/Storage/GroupRoleStorage.php, line 211

Class

GroupRoleStorage
Defines the storage handler class for group role entities.

Namespace

Drupal\group\Entity\Storage

Code

public function createSynchronized($group_type_ids = NULL, $role_ids = NULL) {

  // Load all possible group type IDs if none were provided.
  if (empty($group_type_ids)) {
    $group_type_ids = $this->entityTypeManager
      ->getStorage('group_type')
      ->getQuery()
      ->execute();
  }

  // Return early if there are no group types to create roles for.
  if (empty($group_type_ids)) {
    return;
  }

  /** @var \Drupal\User\RoleInterface[] $user_roles */
  $user_roles = $this->entityTypeManager
    ->getStorage('user_role')
    ->loadMultiple($role_ids);
  $definitions = [];
  foreach (array_keys($user_roles) as $role_id) {

    // We do not synchronize the 'anonymous' or 'authenticated' user roles as
    // they are already taken care of by the 'anonymous' and 'outsider'
    // internal group roles.
    if ($role_id == 'anonymous' || $role_id == 'authenticated') {
      continue;
    }

    // Build a list of group role definitions but do not save them yet so we
    // can check whether they already exist in bulk instead of trying to find
    // out on an individual basis here.
    foreach ($group_type_ids as $group_type_id) {
      $group_role_id = $this->groupRoleSynchronizer
        ->getGroupRoleId($group_type_id, $role_id);
      $definitions[$group_role_id] = [
        'id' => $group_role_id,
        'label' => $user_roles[$role_id]
          ->label(),
        'weight' => $user_roles[$role_id]
          ->getWeight(),
        'internal' => TRUE,
        'audience' => 'outsider',
        'group_type' => $group_type_id,
        'permissions_ui' => FALSE,
        // Adding the user role as an enforced dependency will automatically
        // delete any synchronized group role when its corresponding user role
        // is deleted.
        'dependencies' => [
          'enforced' => [
            'config' => [
              $user_roles[$role_id]
                ->getConfigDependencyName(),
            ],
          ],
        ],
      ];
    }
  }

  // See if the roles we just defined already exist.
  $query = $this
    ->getQuery();
  $query
    ->condition('id', array_keys($definitions));

  // Create the group roles that do not exist yet.
  foreach (array_diff_key($definitions, $query
    ->execute()) as $definition) {
    $this
      ->save($this
      ->create($definition));
  }
}