You are here

public function GroupTypeManager::addGroup in Organic groups 8

Declares a bundle of an entity type as being an OG group.

Parameters

string $entity_type_id: The entity type ID of the bundle to declare as being a group.

string $bundle_id: The bundle ID of the bundle to declare as being a group.

Throws

\InvalidArgumentException Thrown when the given bundle is already a group.

Overrides GroupTypeManagerInterface::addGroup

File

src/GroupTypeManager.php, line 262

Class

GroupTypeManager
A manager to keep track of which entity type/bundles are OG group enabled.

Namespace

Drupal\og

Code

public function addGroup($entity_type_id, $bundle_id) {

  // Throw an error if the entity type is already defined as a group.
  if ($this
    ->isGroup($entity_type_id, $bundle_id)) {
    throw new \InvalidArgumentException("The '{$entity_type_id}' of type '{$bundle_id}' is already a group.");
  }
  $editable = $this->configFactory
    ->getEditable('og.settings');
  $groups = $editable
    ->get('groups');
  $groups[$entity_type_id][] = $bundle_id;

  // @todo Key by bundle ID instead?
  $groups[$entity_type_id] = array_unique($groups[$entity_type_id]);
  $editable
    ->set('groups', $groups);
  $editable
    ->save();

  // Trigger an event upon the new group creation.
  $event = new GroupCreationEvent($entity_type_id, $bundle_id);
  $this->eventDispatcher
    ->dispatch(GroupCreationEventInterface::EVENT_NAME, $event);
  $this->ogRoleManager
    ->createPerBundleRoles($entity_type_id, $bundle_id);
  $this
    ->refreshGroupMap();

  // Routes will need to be rebuilt.
  $this->routeBuilder
    ->setRebuildNeeded();
}