You are here

public function OgResolvedGroupCollection::addGroup in Organic groups 8

Adds a group to the collection.

Groups can be added multiple times by different OgGroupResolver plugins. Each time it is added a 'vote' will be cast in its favor, increasing the chance this group will be chosen as the group context.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $group: The group to add.

string[] $cache_contexts: An optional array of cache contexts to assign to the group.

int $weight: The weight to assign to this vote. If omitted the default weight will be used that is set with ::setVoteWeight().

Overrides OgResolvedGroupCollectionInterface::addGroup

File

src/OgResolvedGroupCollection.php, line 37

Class

OgResolvedGroupCollection
Contains a collection of groups discovered by OgGroupResolver plugins.

Namespace

Drupal\og

Code

public function addGroup(ContentEntityInterface $group, array $cache_contexts = [], $weight = NULL) {
  if ($weight !== NULL && !is_int($weight)) {
    throw new \InvalidArgumentException('Vote weight should be an integer.');
  }
  $key = $this
    ->generateKey($group);
  $this->groupInfo[$key]['entity'] = $group;
  $this->groupInfo[$key]['votes'][] = $weight !== NULL ? $weight : $this
    ->getVoteWeight();
  foreach ($cache_contexts as $cache_context) {
    $this->groupInfo[$key]['cache_contexts'][$cache_context] = $cache_context;
  }
}