This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #483 from Gizra/og-group-member-list
Add a cache tag to invalidate a group's user membership listings
- Loading branch information
Showing
8 changed files
with
646 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
namespace Drupal\og\Plugin\Block; | ||
|
||
use Drupal\Core\Block\BlockBase; | ||
use Drupal\Core\Cache\Cache; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | ||
use Drupal\og\MembershipManagerInterface; | ||
use Drupal\og\OgContextInterface; | ||
use Drupal\og\OgMembershipInterface; | ||
use Drupal\og\OgRoleInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides a block that shows the number of members in the current group. | ||
* | ||
* This block is mainly intended to demonstrate the group membership list cache | ||
* tag but can also be used to show the number of members on group pages. The | ||
* way the text is displayed can be changed by overriding the Twig template. | ||
* | ||
* @Block( | ||
* id = "og_member_count", | ||
* admin_label = @Translation("Group member count") | ||
* ) | ||
*/ | ||
class MemberCountBlock extends BlockBase implements ContainerFactoryPluginInterface { | ||
|
||
/** | ||
* The OG context provider. | ||
* | ||
* @var \Drupal\og\OgContextInterface | ||
*/ | ||
protected $ogContext; | ||
|
||
/** | ||
* The membership manager. | ||
* | ||
* @var \Drupal\og\MembershipManagerInterface | ||
*/ | ||
protected $membershipManager; | ||
|
||
/** | ||
* Constructs a MemberCountBlock object. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing information about the plugin instance. | ||
* @param string $plugin_id | ||
* The plugin ID for the plugin instance. | ||
* @param string $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Drupal\og\OgContextInterface $og_context | ||
* The OG context provider. | ||
* @param \Drupal\og\MembershipManagerInterface $membership_manager | ||
* The membership manager. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, OgContextInterface $og_context, MembershipManagerInterface $membership_manager) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
|
||
$this->ogContext = $og_context; | ||
$this->membershipManager = $membership_manager; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | ||
return new static( | ||
$configuration, | ||
$plugin_id, | ||
$plugin_definition, | ||
$container->get('og.context'), | ||
$container->get('og.membership_manager') | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function defaultConfiguration() { | ||
return [ | ||
'count_blocked_users' => FALSE, | ||
'count_pending_users' => FALSE, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function blockForm($form, FormStateInterface $form_state) { | ||
$form['count_blocked_users'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $this->t('Count blocked users'), | ||
'#default_value' => $this->configuration['count_blocked_users'], | ||
]; | ||
|
||
$form['count_pending_users'] = [ | ||
'#type' => 'checkbox', | ||
'#title' => $this->t('Count pending users'), | ||
'#default_value' => $this->configuration['count_pending_users'], | ||
]; | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function blockSubmit($form, FormStateInterface $form_state) { | ||
foreach (array_keys($this->defaultConfiguration()) as $setting) { | ||
$this->configuration[$setting] = $form_state->getValue($setting); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function build() { | ||
// Do not render anything if there is no group in the current context. | ||
$group = $this->ogContext->getGroup(); | ||
if (empty($group)) { | ||
return []; | ||
} | ||
|
||
$states = [OgMembershipInterface::STATE_ACTIVE]; | ||
|
||
if ($this->configuration['count_blocked_users']) { | ||
$states[] = OgMembershipInterface::STATE_BLOCKED; | ||
} | ||
|
||
if ($this->configuration['count_pending_users']) { | ||
$states[] = OgMembershipInterface::STATE_PENDING; | ||
} | ||
|
||
$membership_ids = $this->membershipManager->getGroupMembershipIdsByRoleNames($group, [OgRoleInterface::AUTHENTICATED], $states); | ||
|
||
return [ | ||
'#theme' => 'og_member_count', | ||
'#count' => count($membership_ids), | ||
'#group' => $group, | ||
'#membership_states' => $states, | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheTags() { | ||
$tags = parent::getCacheTags(); | ||
|
||
$group = $this->ogContext->getGroup(); | ||
if (!empty($group)) { | ||
$tags = Cache::mergeTags(Cache::buildTags(OgMembershipInterface::GROUP_MEMBERSHIP_LIST_CACHE_TAG_PREFIX, $group->getCacheTagsToInvalidate()), $tags); | ||
} | ||
|
||
return $tags; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheContexts() { | ||
return Cache::mergeContexts(parent::getCacheContexts(), ['og_group_context']); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{# | ||
/** | ||
* @file | ||
* Default theme implementation to show the member count of a group. | ||
* | ||
* Available variables: | ||
* - count: The number of members in the group. | ||
* - membership_states: An array of membership states included in the count. | ||
* - group_label: The group label. | ||
* | ||
* @see \Drupal\og\Plugin\Block\MemberCountBlock | ||
* | ||
* @ingroup themeable | ||
*/ | ||
#} | ||
<p> | ||
{% trans %} | ||
{{ group_label }} has 1 member. | ||
{% plural count %} | ||
{{ group_label }} has {{ count }} members. | ||
{% endtrans %} | ||
</p> |
Oops, something went wrong.