You are here

protected function GroupMenuConfigOverrides::getUserGroupMenuIdsByGroupTypes in Group Menu 8

Get a users group menu IDs for a list of group types.

Parameters

array $group_types: An array of group types with the ID as key.

Return value

array An array of menu IDs.

1 call to GroupMenuConfigOverrides::getUserGroupMenuIdsByGroupTypes()
GroupMenuConfigOverrides::loadOverrides in src/GroupMenuConfigOverrides.php
Returns config overrides.

File

src/GroupMenuConfigOverrides.php, line 176

Class

GroupMenuConfigOverrides
Group menu configuration overrides.

Namespace

Drupal\groupmenu

Code

protected function getUserGroupMenuIdsByGroupTypes(array $group_types) {
  $group_types_cid = md5(implode('-', $group_types));
  if (isset($this->userGroupMenuIds[$this->currentUser
    ->id()][$group_types_cid])) {
    return $this->userGroupMenuIds[$this->currentUser
      ->id()][$group_types_cid];
  }
  $cid = 'groupmenu:user_group_menu_ids:' . $this->currentUser
    ->id() . ':' . $group_types_cid;
  $persistent_cache = $this->cache
    ->get($cid);
  if ($persistent_cache && $persistent_cache->valid) {
    $this->userGroupMenuIds[$this->currentUser
      ->id()][$group_types_cid] = $persistent_cache->data;
    return $this->userGroupMenuIds[$this->currentUser
      ->id()][$group_types_cid];
  }

  // We can't use dependency injection for entity type manager, since this
  // will cause circular dependencies.
  $entity_type_manager = \Drupal::service('entity_type.manager');
  $plugin_id = 'group_menu:menu';
  $group_content_types = $entity_type_manager
    ->getStorage('group_content_type')
    ->loadByProperties([
    'content_plugin' => $plugin_id,
    'group_type' => array_keys($group_types),
  ]);
  if (empty($group_content_types)) {
    return [];
  }
  $group_contents = $entity_type_manager
    ->getStorage('group_content')
    ->loadByProperties([
    'type' => array_keys($group_content_types),
  ]);

  // Check access and add menus to config.
  $this->userGroupMenuIds[$this->currentUser
    ->id()][$group_types_cid] = [];
  foreach ($group_contents as $group_content) {

    /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
    if ($group_content && $group_content
      ->getGroup()
      ->hasPermission("update {$plugin_id} entity", $this->currentUser) && ($entity = $group_content
      ->getEntity())) {
      $this->userGroupMenuIds[$this->currentUser
        ->id()][$group_types_cid][] = $entity
        ->id();
    }
  }
  $this->cache
    ->set($cid, $this->userGroupMenuIds[$this->currentUser
    ->id()][$group_types_cid]);
  return $this->userGroupMenuIds[$this->currentUser
    ->id()][$group_types_cid];
}