You are here

public function RouteGroupContentResolver::resolve in Organic groups 8

Resolves groups within the plugin's domain.

Parameters

\Drupal\og\OgResolvedGroupCollectionInterface $collection: A collection of groups that were resolved by previous plugins. If the plugin discovers new groups, it may add these to this collection. A plugin may also remove groups from the collection that were previously discovered by other plugins, if it finds out that certain groups are incompatible with the current state in the plugin's domain.

Overrides OgGroupResolverInterface::resolve

File

src/Plugin/OgGroupResolver/RouteGroupContentResolver.php, line 89

Class

RouteGroupContentResolver
Resolves the group from the route.

Namespace

Drupal\og\Plugin\OgGroupResolver

Code

public function resolve(OgResolvedGroupCollectionInterface $collection) {
  $entity = $this
    ->getContentEntity();

  // Check if the route entity is group content by checking if it has a group
  // audience field.
  if ($entity && $this->groupAudienceHelper
    ->hasGroupAudienceField($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $groups = $this->membershipManager
      ->getGroups($entity);

    // The groups are returned as a two-dimensional array. Flatten it.
    $groups = array_reduce($groups, 'array_merge', []);
    foreach ($groups as $group) {
      $collection
        ->addGroup($group, [
        'route',
      ]);
    }
  }
}