You are here

public function GroupRoleStorage::createInternal in Group 8

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

Creates internal group roles for group types.

Internal group roles are the bare minimum group roles that a group type needs to function properly, such as the Member group role.

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.

Overrides GroupRoleStorageInterface::createInternal

File

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

Class

GroupRoleStorage
Defines the storage handler class for group role entities.

Namespace

Drupal\group\Entity\Storage

Code

public function createInternal($group_type_ids = NULL) {

  /** @var \Drupal\group\Entity\GroupTypeInterface[] $group_types */
  $group_types = $this->entityTypeManager
    ->getStorage('group_type')
    ->loadMultiple($group_type_ids);

  // Return early if there are no group types to create roles for.
  if (empty($group_types)) {
    return;
  }
  $definitions = [];
  foreach ($group_types as $group_type_id => $group_type) {

    // 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.
    $definitions[$group_type
      ->getAnonymousRoleId()] = [
      'id' => $group_type
        ->getAnonymousRoleId(),
      'label' => $this
        ->t('Anonymous'),
      'weight' => -102,
      'internal' => TRUE,
      'audience' => 'anonymous',
      'group_type' => $group_type_id,
    ];
    $definitions[$group_type
      ->getOutsiderRoleId()] = [
      'id' => $group_type
        ->getOutsiderRoleId(),
      'label' => $this
        ->t('Outsider'),
      'weight' => -101,
      'internal' => TRUE,
      'audience' => 'outsider',
      'group_type' => $group_type_id,
    ];
    $definitions[$group_type
      ->getMemberRoleId()] = [
      'id' => $group_type
        ->getMemberRoleId(),
      'label' => $this
        ->t('Member'),
      'weight' => -100,
      'internal' => TRUE,
      'group_type' => $group_type_id,
    ];
  }

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