Skip to content
This repository has been archived by the owner on Aug 18, 2024. It is now read-only.

Clarify how to work with all membership states. #449

Merged
merged 2 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions og.module
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,7 @@ function og_entity_insert(EntityInterface $entity) {

// Other modules that implement hook_entity_insert() might already have
// created a membership ahead of us.
if (!Og::getMembership($entity, $entity->getOwner(), [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
])) {
if (!Og::getMembership($entity, $entity->getOwner(), OgMembershipInterface::ALL_STATES)) {
$membership = Og::createMembership($entity, $entity->getOwner());
$membership->save();
}
Expand Down
8 changes: 1 addition & 7 deletions src/Cache/Context/OgMembershipStateCacheContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,8 @@ public function getContext() {
return self::NO_CONTEXT;
}

$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];

/** @var \Drupal\og\OgMembershipInterface $membership */
$membership = $this->membershipManager->getMembership($group, $this->user, $states);
$membership = $this->membershipManager->getMembership($group, $this->user, OgMembershipInterface::ALL_STATES);
return $membership ? $membership->getState() : self::NO_CONTEXT;
}

Expand Down
8 changes: 1 addition & 7 deletions src/Controller/SubscriptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,7 @@ public function subscribe($entity_type_id, EntityInterface $group, OgMembershipT
public function unsubscribe(ContentEntityInterface $group) {
$user = $this->currentUser();

$states = [
OgMembershipInterface::STATE_ACTIVE,
OgMembershipInterface::STATE_PENDING,
OgMembershipInterface::STATE_BLOCKED,
];

if (!$membership = Og::getMembership($group, $user, $states)) {
if (!$membership = Og::getMembership($group, $user, OgMembershipInterface::ALL_STATES)) {
// User is not a member.
throw new AccessDeniedHttpException();
}
Expand Down
11 changes: 6 additions & 5 deletions src/MembershipManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public function getUserGroups(AccountInterface $user, array $states = [OgMembers
* {@inheritdoc}
*/
public function getMemberships(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
// When an empty array is passed, retrieve memberships with all possible
// states.
$states = $states ?: OgMembership::ALL_STATES;

// Get a string identifier of the states, so we can retrieve it from cache.
sort($states);
$states_identifier = implode('|', array_unique($states));
Expand All @@ -101,11 +105,8 @@ public function getMemberships(AccountInterface $user, array $states = [OgMember
$query = $this->entityTypeManager
->getStorage('og_membership')
->getQuery()
->condition('uid', $user->id());

if ($states) {
$query->condition('state', $states, 'IN');
}
->condition('uid', $user->id())
->condition('state', $states, 'IN');

$results = $query->execute();

Expand Down
8 changes: 6 additions & 2 deletions src/MembershipManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public function getUserGroups(AccountInterface $user, array $states = [OgMembers
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get groups for.
* @param array $states
* (optional) Array with the state to return. Defaults to active.
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
* state, pass `OgMembershipInterface::ALL_STATES`.
*
* @return \Drupal\og\OgMembershipInterface[]
* An array of OgMembership entities, keyed by ID.
Expand All @@ -76,7 +78,9 @@ public function getMemberships(AccountInterface $user, array $states = [OgMember
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get the membership for.
* @param array $states
* (optional) Array with the state to return. Defaults to active.
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
* state, pass `OgMembershipInterface::ALL_STATES`.
*
* @return \Drupal\og\OgMembershipInterface|null
* The OgMembership entity. NULL will be returned if no membership is
Expand Down
12 changes: 8 additions & 4 deletions src/Og.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ public static function createField($plugin_id, $entity_type, $bundle, array $set
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get groups for.
* @param array $states
* (optional) Array with the state to return. Defaults to active.
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
* state, pass `OgMembershipInterface::ALL_STATES`.
*
* @return \Drupal\og\Entity\OgMembership[]
* @return \Drupal\og\OgMembershipInterface[]
* An array of OgMembership entities, keyed by ID.
*/
public static function getMemberships(AccountInterface $user, array $states = [OgMembershipInterface::STATE_ACTIVE]) {
Expand All @@ -152,9 +154,11 @@ public static function getMemberships(AccountInterface $user, array $states = [O
* @param \Drupal\Core\Session\AccountInterface $user
* The user to get the membership for.
* @param array $states
* (optional) Array with the state to return. Defaults to active.
* (optional) Array with the states to return. Defaults to only returning
* active memberships. In order to retrieve all memberships regardless of
* state, pass `OgMembershipInterface::ALL_STATES`.
*
* @return \Drupal\og\Entity\OgMembership|null
* @return \Drupal\og\OgMembershipInterface|null
* The OgMembership entity. NULL will be returned if no membership is
* available that matches the passed in $states.
*/
Expand Down
9 changes: 9 additions & 0 deletions src/OgMembershipInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,15 @@ interface OgMembershipInterface extends ContentEntityInterface, EntityOwnerInter
*/
const STATE_BLOCKED = 'blocked';

/**
* An array containing all possible group membership states.
*/
const ALL_STATES = [
self::STATE_ACTIVE,
self::STATE_PENDING,
self::STATE_BLOCKED,
];

/**
* The default group membership type that is the bundle of group membership.
*/
Expand Down