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

Add a cache tag to invalidate a group's user membership listings #483

Merged
merged 7 commits into from
Apr 30, 2019

Conversation

pfrenssen
Copy link
Contributor

Fixes #472.

This also includes a member count block that demonstrates the use of the cache tag, but is fully themeable and usable in production sites to display member counts in group pages.

This also includes a block to display the member count of a group as a
demonstration of the cache tag.
$tags = Cache::buildTags('og-group-membership-list', $this->getGroup()->getCacheTagsToInvalidate());
Cache::invalidateTags($tags);
if ($group = $this->getGroup()) {
$tags = Cache::buildTags('og-group-membership-list', $group->getCacheTagsToInvalidate());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use a constant instead of 'og-group-membership-list'?

og.module Show resolved Hide resolved
@pfrenssen pfrenssen merged commit 9d849da into 8.x-1.x Apr 30, 2019
@pfrenssen pfrenssen deleted the og-group-member-list branch April 30, 2019 15:07
Copy link
Collaborator

@claudiu-cristea claudiu-cristea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is already merged. However, I still detected some points

* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always have to account the parent return value, even in this case that returns an empty array. This is because the parent might return something in the future or at some point we switch to other base class:

return [
  ...
] + parent:: defaultConfiguration();

* Default template: og-member-count.html.twig.
*
* @param array $variables
* An associative array containing:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The array contains also the membership_states key but this is not detailed below.

* - count: An integer representing the number of members in the group.
* - group: The group, which is a content entity.
*/
function template_preprocess_og_member_count(array &$variables) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the reason of this preprocess. We can pass the label directly from the block plugin.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended to demonstrate how and end user would change this block into something useful for them.

* 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The group variable is also available as we don't unset it in template_preprocess_og_member_count(). If we don't need this variable, we probably want to pass the label from the beginning, in the block plugin and remove the template preprocess function. Or we add here the group too.

*/
protected function assertMemberCount($group_key, $expected_count) {
$expected_string = (string) $this->formatPlural($expected_count, '@label has 1 member.', '@label has @count members', ['@label' => $this->groups[$group_key]->label()]);
$this->assertTrue(strpos($this->renderBlock($group_key), $expected_string) !== FALSE);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be written in more meaningful way:

$this->assertContains($expected_string, (string) $this->renderBlock($group_key));

@MPParsley MPParsley restored the og-group-member-list branch May 2, 2019 13:45
@MPParsley MPParsley mentioned this pull request May 2, 2019
@MPParsley
Copy link
Collaborator

@claudiu-cristea, please review this PR: #486

@claudiu-cristea
Copy link
Collaborator

@MPParsley, thank you. I just did a review. I see only one aspect.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide an og-group-member-list cache tag that is invalidated when a membership changes
4 participants