You are here

public function SubscriptionController::unsubscribe in Organic groups 8

Unsubscribe a user from group.

Parameters

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

Return value

mixed Redirect user or show access denied if they are not allowed to subscribe, otherwise provide an un-subscribe confirmation form.

1 string reference to 'SubscriptionController::unsubscribe'
og.routing.yml in ./og.routing.yml
og.routing.yml

File

src/Controller/SubscriptionController.php, line 164

Class

SubscriptionController
Controller for OG subscription routes.

Namespace

Drupal\og\Controller

Code

public function unsubscribe(ContentEntityInterface $group) {
  $user = $this
    ->currentUser();
  if (!($membership = Og::getMembership($group, $user, OgMembershipInterface::ALL_STATES))) {

    // User is not a member.
    throw new AccessDeniedHttpException();
  }
  if ($membership
    ->getState() == OgMembershipInterface::STATE_BLOCKED) {

    // User is a blocked member.
    throw new AccessDeniedHttpException();
  }
  if ($group instanceof EntityOwnerInterface && $group
    ->getOwnerId() == $user
    ->id()) {

    // The user is the manager of the group.
    $this->messenger
      ->addMessage($this
      ->t('As the manager of %group, you can not leave the group.', [
      '%group' => $group
        ->label(),
    ]));
    return new RedirectResponse($group
      ->toUrl()
      ->setAbsolute()
      ->toString());
  }
  $form = $this
    ->entityFormBuilder()
    ->getForm($membership, 'unsubscribe');
  return $form;
}