You are here

function og_entity_insert in Organic groups 8

Same name and namespace in other branches
  1. 7.2 og.module \og_entity_insert()
  2. 7 og.module \og_entity_insert()

Implements hook_entity_insert().

Subscribe the group manager.

File

./og.module, line 33

Code

function og_entity_insert(EntityInterface $entity) {

  // Invalidate cache tags if new group content is created.
  og_invalidate_group_content_cache_tags($entity);
  if (!Og::isGroup($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {

    // Not a group entity.
    return;
  }
  if (!$entity instanceof EntityOwnerInterface) {
    return;
  }
  $owner = $entity
    ->getOwner();
  if (empty($owner) || $owner
    ->isAnonymous()) {

    // User is anonymous, se we cannot set a membership for them.
    return;
  }

  // Other modules that implement hook_entity_insert() might already have
  // created a membership ahead of us.
  if (!Og::getMembership($entity, $entity
    ->getOwner(), OgMembershipInterface::ALL_STATES)) {
    $membership = Og::createMembership($entity, $entity
      ->getOwner());
    $membership
      ->save();
  }
}