You are here

public function RequestQueryArgumentResolver::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/RequestQueryArgumentResolver.php, line 101

Class

RequestQueryArgumentResolver
Resolves the group from the query arguments on the request.

Namespace

Drupal\og\Plugin\OgGroupResolver

Code

public function resolve(OgResolvedGroupCollectionInterface $collection) {

  // Check if our arguments are present on the request.
  $query = $this->requestStack
    ->getCurrentRequest()->query;
  if ($query
    ->has(self::GROUP_TYPE_ARGUMENT) && $query
    ->has(self::GROUP_ID_ARGUMENT)) {
    try {
      $storage = $this->entityTypeManager
        ->getStorage($query
        ->get(self::GROUP_TYPE_ARGUMENT));
    } catch (InvalidPluginDefinitionException $e) {

      // Invalid entity type specified, cannot resolve group.
      return;
    }

    // Load the entity and check if it is a group.

    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    if ($entity = $storage
      ->load($query
      ->get(self::GROUP_ID_ARGUMENT))) {
      if ($this->groupTypeManager
        ->isGroup($entity
        ->getEntityTypeId(), $entity
        ->bundle())) {

        // Only add a vote for the group if it already has been discovered by
        // a previous plugin. This will make sure that users cannot fake a
        // group context by messing with the query arguments.
        if ($collection
          ->hasGroup($entity)) {
          $collection
            ->addGroup($entity, [
            'url',
          ]);
        }
      }
    }
  }
}