You are here

protected function OgRoleCacheContext::getMembershipsFromEntities in Organic groups 8

Returns membership information by iterating over membership entities.

This method uses pure Entity API methods to retrieve the data. This is slow but also works with NoSQL databases.

Return value

array[][] An array containing membership information for the current user. The data is in the format [$entity_type_id][$entity_id][$role_name].

1 call to OgRoleCacheContext::getMembershipsFromEntities()
OgRoleCacheContext::getContext in src/Cache/Context/OgRoleCacheContext.php
Returns the string representation of the cache context.

File

src/Cache/Context/OgRoleCacheContext.php, line 221

Class

OgRoleCacheContext
Defines a cache context service for the OG roles of the current user.

Namespace

Drupal\og\Cache\Context

Code

protected function getMembershipsFromEntities() : array {
  $memberships = [];
  foreach ($this->membershipManager
    ->getMemberships($this->user
    ->id()) as $membership) {

    // Derive the role names from the role IDs. This is faster than loading
    // the OgRole object from the membership.
    $role_names = array_map(function (string $role_id) use ($membership) : string {
      $pattern = preg_quote("{$membership->getGroupEntityType()}-{$membership->getGroupBundle()}-");
      preg_match("/{$pattern}(.+)/", $role_id, $matches);
      return $matches[1];
    }, $membership
      ->getRolesIds());
    if ($role_names) {
      $memberships[$membership
        ->getGroupEntityType()][$membership
        ->getGroupId()] = $role_names;
    }
  }
  return $memberships;
}