You are here

public function GroupHierarchyManager::addSubgroup in Subgroup (Graph) 1.0.x

Relates one group to another as a subgroup.

Parameters

\Drupal\group\Entity\GroupContentInterface $group_content: The group content representing the subgroup relationship.

Overrides GroupHierarchyManagerInterface::addSubgroup

File

src/GroupHierarchyManager.php, line 60

Class

GroupHierarchyManager
Manages the relationship between groups (as subgroups).

Namespace

Drupal\ggroup

Code

public function addSubgroup(GroupContentInterface $group_content) {
  $plugin = $group_content
    ->getContentPlugin();
  if ($plugin
    ->getEntityTypeId() !== 'group') {
    throw new \InvalidArgumentException('Given group content entity does not represent a subgroup relationship.');
  }
  $parent_group = $group_content
    ->getGroup();

  /** @var \Drupal\group\Entity\GroupInterface $child_group */
  $child_group = $group_content
    ->getEntity();
  if ($parent_group
    ->id() === NULL) {
    throw new \InvalidArgumentException('Parent group must be saved before it can be related to another group.');
  }
  if ($child_group
    ->id() === NULL) {
    throw new \InvalidArgumentException('Child group must be saved before it can be related to another group.');
  }
  $new_edge_id = $this->groupGraphStorage
    ->addEdge($parent_group
    ->id(), $child_group
    ->id());

  // @todo Invalidate some kind of cache?
}