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 132
Default argument for current user memberships #565
Closed
Closed
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
1e6f95f
Default argument for current user memberships.
dc2724c
Standardise name of user property
f6e0e52
Add missing getGroup method
zerolab e9ea156
Tweak docblocks and comments
zerolab 2591f05
Merge pull request #564 from tombola/default-argument-membership
MPParsley 40ff05f
Fix deprecation
MPParsley 7dc3e43
Renamed og_user to user
MPParsley fc0e259
Removed whitespace
MPParsley 4f2f5a1
Update src/Plugin/views/argument_default/Membership.php
MPParsley d27b965
Cherry pick #563 to fix tests
MPParsley 7922152
Default argument for current user memberships.
4946448
Standardise name of user property
8de21d5
Add missing getGroup method
zerolab c9a2fca
Tweak docblocks and comments
zerolab 0a21336
Fix deprecation
MPParsley b19edb7
Renamed og_user to user
MPParsley 9b429c1
Removed whitespace
MPParsley c25d324
Update src/Plugin/views/argument_default/Membership.php
MPParsley 35656a9
Cherry pick #563 to fix tests
MPParsley afd38d9
Rebase
47655e6
Apply suggestions from code review
MPParsley 6afb590
Fixed CS
MPParsley 768cd09
Merge pull request #568 from Gizra/8.x-1.x
MPParsley 07a5f39
Merge pull request #607 from Gizra/8.x-1.x
MPParsley 2a54d3e
Make entity and role id configurable
MPParsley 82cc63f
Removed whitespace
MPParsley 7b84ff2
Add option to set role ids
MPParsley 2c8d6eb
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley ef7befd
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley fab6a98
Add strict types
MPParsley 1b2b8d8
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 35e1bb8
Remove comment
MPParsley 802a9d6
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley ac60e8d
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 0f227c6
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 74695ad
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 8f5f282
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 088ccc7
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley 14a500f
Merge branch '8.x-1.x' into feature/default-argument-membership
MPParsley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\og\Plugin\views\argument_default; | ||
|
||
use Drupal\Core\Cache\Cache; | ||
use Drupal\Core\Cache\CacheableDependencyInterface; | ||
use Drupal\Core\Entity\ContentEntityInterface; | ||
use Drupal\Core\Form\FormStateInterface; | ||
use Drupal\Core\Plugin\Context\ContextProviderInterface; | ||
use Drupal\og\OgMembershipInterface; | ||
use Drupal\views\Plugin\views\argument_default\ArgumentDefaultPluginBase; | ||
use Drupal\og\MembershipManagerInterface; | ||
use Drupal\Core\Session\AccountInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides the group memberships from the current context. | ||
* | ||
* @ViewsArgumentDefault( | ||
* id = "og_group_membership", | ||
* title = @Translation("Group memberships from current user") | ||
* ) | ||
*/ | ||
class Membership extends ArgumentDefaultPluginBase implements CacheableDependencyInterface { | ||
|
||
/** | ||
* The OG context provider. | ||
* | ||
* @var \Drupal\Core\Plugin\Context\ContextProviderInterface | ||
*/ | ||
protected $ogContext; | ||
|
||
/** | ||
* The OG membership manager. | ||
* | ||
* @var \Drupal\og\MembershipManagerInterface | ||
*/ | ||
protected $ogMembership; | ||
|
||
/** | ||
* The user to be evaluated. | ||
* | ||
* @var \Drupal\Core\Session\AccountInterface | ||
*/ | ||
protected $ogUser; | ||
|
||
/** | ||
* Constructs a new Membership instance. | ||
* | ||
* @param array $configuration | ||
* A configuration array containing information about the plugin instance. | ||
* @param string $plugin_id | ||
* The plugin_id for the plugin instance. | ||
* @param mixed $plugin_definition | ||
* The plugin implementation definition. | ||
* @param \Drupal\Core\Plugin\Context\ContextProviderInterface $og_context | ||
* The OG context provider. | ||
* @param \Drupal\og\MembershipManagerInterface $og_membership | ||
* The OG membership manager. | ||
* @param \Drupal\Core\Session\AccountInterface $user | ||
* The user to be evaluated. | ||
*/ | ||
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextProviderInterface $og_context, MembershipManagerInterface $og_membership, AccountInterface $user) { | ||
parent::__construct($configuration, $plugin_id, $plugin_definition); | ||
|
||
$this->ogContext = $og_context; | ||
$this->ogMembership = $og_membership; | ||
$this->ogUser = $user; | ||
} | ||
|
||
/** | ||
* {@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'), | ||
$container->get('current_user')->getAccount() | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function defineOptions() { | ||
$options = parent::defineOptions(); | ||
$options['og_group_membership'] = ['default' => '']; | ||
$options['entity_type'] = ['default' => 'node']; | ||
$options['role_ids'] = ['default' => '']; | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildOptionsForm(&$form, FormStateInterface $form_state) { | ||
parent::buildOptionsForm($form, $form_state); | ||
$form['og_group_membership'] = []; | ||
$form['entity_type'] = [ | ||
'#type' => 'text', | ||
'#title' => $this->t('Entity type'), | ||
'#default_value' => $this->options['entity_type'], | ||
]; | ||
$form['role_ids'] = [ | ||
'#type' => 'text', | ||
'#title' => $this->t('Roles'), | ||
'#default_value' => $this->options['role_ids'], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getArgument() { | ||
return implode(',', $this->getCurrentUserGroupIds()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheMaxAge() { | ||
return Cache::PERMANENT; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheContexts() { | ||
// This cache context is the best thing we have right now. | ||
// og_role takes in consideration the user memberships and | ||
// the roles held in the corresponding groups, and while it | ||
// is one level too granular, i.e. the context will be more | ||
// fragmented than strictly needed, it works. | ||
return ['og_role']; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getCacheTags() { | ||
$group = $this->getGroup(); | ||
if ($group instanceof ContentEntityInterface) { | ||
$tag = $group->getEntityTypeId() . ':' . $group->id(); | ||
return Cache::buildTags('og-group-content', [$tag]); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Returns groups that current user is a member of. | ||
* | ||
* @return array | ||
* An array of groups, or an empty array if no group is found. | ||
*/ | ||
protected function getCurrentUserGroupIds() { | ||
$entity_type = $this->options['entity_type']; | ||
$role_ids = []; | ||
if ($this->options['role_ids'] !== '') { | ||
$role_ids = explode(',', $this->options['role_ids']); | ||
} | ||
$groups = $this->ogMembership->getUserGroupIdsByRoleIds($this->ogUser->id(), $role_ids, [OgMembershipInterface::STATE_ACTIVE], FALSE); | ||
if (!empty($groups) && isset($groups[$entity_type])) { | ||
return $groups[$entity_type]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* Returns the group from the runtime context. | ||
* | ||
* @return \Drupal\Core\Entity\ContentEntityInterface|null | ||
* The group from context if found. | ||
*/ | ||
protected function getGroup() { | ||
$contexts = $this->ogContext->getRuntimeContexts(['og']); | ||
if (!empty($contexts['og']) && $group = $contexts['og']->getContextValue()) { | ||
if ($group instanceof ContentEntityInterface) { | ||
return $group; | ||
} | ||
} | ||
Comment on lines
+184
to
+188
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's return early, instead of indenting the IFs |
||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand the "is one level too granular" part. What do you mean here?