You are here

public function MembershipManager::createMembership in Organic groups 8

Creates an OG membership.

Parameters

\Drupal\Core\Entity\EntityInterface $group: The group entity.

\Drupal\user\UserInterface $user: The user object.

string $membership_type: (optional) The membership type. Defaults to \Drupal\og\OgMembershipInterface::TYPE_DEFAULT.

Return value

\Drupal\og\OgMembershipInterface The unsaved membership object.

Overrides MembershipManagerInterface::createMembership

File

src/MembershipManager.php, line 247

Class

MembershipManager
Service for managing memberships and group content.

Namespace

Drupal\og

Code

public function createMembership(EntityInterface $group, UserInterface $user, $membership_type = OgMembershipInterface::TYPE_DEFAULT) {

  /** @var \Drupal\user\UserInterface $user */

  /** @var \Drupal\og\OgMembershipInterface $membership */
  $membership = OgMembership::create([
    'type' => $membership_type,
  ]);
  $membership
    ->setOwner($user)
    ->setGroup($group);
  return $membership;
}