public function SubscriptionController::subscribe in Organic groups 8
Subscribe a user to group.
Parameters
string $entity_type_id: The entity type of the group entity.
\Drupal\Core\Entity\EntityInterface $group: The entity ID of the group entity.
\Drupal\og\OgMembershipTypeInterface $membership_type: The membership type to be used for creating the membership.
Return value
mixed Redirect user or show access denied if they are not allowed to subscribe, otherwise provide a subscribe confirmation form.
1 string reference to 'SubscriptionController::subscribe'
File
- src/
Controller/ SubscriptionController.php, line 78
Class
- SubscriptionController
- Controller for OG subscription routes.
Namespace
Drupal\og\ControllerCode
public function subscribe($entity_type_id, EntityInterface $group, OgMembershipTypeInterface $membership_type) {
if (!$group instanceof ContentEntityInterface) {
// Not a valid entity.
throw new AccessDeniedHttpException();
}
if (!Og::isGroup($entity_type_id, $group
->bundle())) {
// Not a valid group.
throw new AccessDeniedHttpException();
}
$user = $this
->entityTypeManager()
->getStorage('user')
->load($this
->currentUser()
->id());
if ($user
->isAnonymous()) {
// Anonymous user can't request membership.
$destination = $this
->getDestinationArray();
$user_login_url = Url::fromRoute('user.login', [], $destination)
->toString();
if ($this
->config('user.settings')
->get('register') === UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
$params = [
':login' => $user_login_url,
];
$this->messenger
->addMessage($this
->t('In order to join any group, you must <a href=":login">login</a>. After you have successfully done so, you will need to request membership again.', $params));
}
else {
$user_register_url = Url::fromRoute('user.register', [], $destination)
->toString();
$params = [
':register' => $user_register_url,
':login' => $user_login_url,
];
$this->messenger
->addMessage($this
->t('In order to join any group, you must <a href=":login">login</a> or <a href=":register">register</a> a new account. After you have successfully done so, you will need to request membership again.', $params));
}
return new RedirectResponse(Url::fromRoute('user.page')
->setAbsolute(TRUE)
->toString());
}
$redirect = FALSE;
$message = '';
$params = [
'@user' => $user
->getDisplayName(),
];
// Show the group name only if user has access to it.
$params['@group'] = $group
->access('view', $user) ? $group
->label() : $this
->t('Private group');
if (Og::isMemberBlocked($group, $user)) {
// User is blocked, access denied.
throw new AccessDeniedHttpException();
}
if (Og::isMemberPending($group, $user)) {
// User is pending, return them back.
$message = $this
->t('You already have a pending membership for the the group @group.', $params);
$redirect = TRUE;
}
if (Og::isMember($group, $user)) {
// User is already a member, return them back.
$message = $this
->t('You are already a member of the group @group.', $params);
$redirect = TRUE;
}
if ($redirect) {
$this->messenger
->addMessage($message, 'warning');
return new RedirectResponse($group
->toUrl()
->setAbsolute(TRUE)
->toString());
}
$subscribe = $this->ogAccess
->userAccess($group, 'subscribe');
$subscribe_without_approval = $this->ogAccess
->userAccess($group, 'subscribe without approval');
if (!$subscribe
->isAllowed() && !$subscribe_without_approval
->isAllowed()) {
throw new AccessDeniedHttpException();
}
$membership = Og::createMembership($group, $user, $membership_type
->id());
$form = $this
->entityFormBuilder()
->getForm($membership, 'subscribe');
return $form;
}