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

Commit

Permalink
Clarify how to work with all membership states.
Browse files Browse the repository at this point in the history
Fixes #335 and #336.
  • Loading branch information
pfrenssen committed Dec 11, 2018
1 parent 07df1f5 commit ac0e39d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 31 deletions.
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::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::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::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::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
10 changes: 7 additions & 3 deletions src/MembershipManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ 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::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::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::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::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
5 changes: 5 additions & 0 deletions src/OgMembershipInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ interface OgMembershipInterface extends ContentEntityInterface, EntityOwnerInter
*/
const STATE_BLOCKED = 'blocked';

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

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

0 comments on commit ac0e39d

Please sign in to comment.