From 1e6f95fce80b9d69412c727b4e99297046b651e2 Mon Sep 17 00:00:00 2001 From: autopoietic Date: Thu, 13 Apr 2017 11:34:42 +0100 Subject: [PATCH 1/4] Default argument for current user memberships. --- .../views/argument_default/Membership.php | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 src/Plugin/views/argument_default/Membership.php diff --git a/src/Plugin/views/argument_default/Membership.php b/src/Plugin/views/argument_default/Membership.php new file mode 100644 index 000000000..0eed024f9 --- /dev/null +++ b/src/Plugin/views/argument_default/Membership.php @@ -0,0 +1,149 @@ +ogContext = $og_context; + $this->ogMembership = $og_membership; + $this->user = $og_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' => '']; + + return $options; + } + + /** + * {@inheritdoc} + */ + public function buildOptionsForm(&$form, FormStateInterface $form_state) { + $form['og_group_membership'] = []; + } + + /** + * {@inheritdoc} + */ + public function getArgument() { + // Currently restricted to node entities. + return implode(',', $this->getCurrentUserGroupIds('node')); + } + + /** + * {@inheritdoc} + */ + public function getCacheMaxAge() { + return Cache::PERMANENT; + } + + /** + * {@inheritdoc} + */ + public function getCacheContexts() { + 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 = 'node') { + $groups = $this->ogMembership->getUserGroupIds($this->user); + if (!empty($groups) && isset($groups[$entity_type])) { + return $groups[$entity_type]; + } + return []; + } + +} From dc2724cdb73c2fde83fa8da721027248893de798 Mon Sep 17 00:00:00 2001 From: autopoietic Date: Wed, 19 Apr 2017 10:06:08 +0100 Subject: [PATCH 2/4] Standardise name of user property --- src/Plugin/views/argument_default/Membership.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Plugin/views/argument_default/Membership.php b/src/Plugin/views/argument_default/Membership.php index 0eed024f9..b7e0c2038 100644 --- a/src/Plugin/views/argument_default/Membership.php +++ b/src/Plugin/views/argument_default/Membership.php @@ -41,7 +41,7 @@ class Membership extends ArgumentDefaultPluginBase implements CacheableDependenc * * @var \Drupal\Core\Session\AccountInterface */ - protected $user; + protected $ogUser; /** * Constructs a new User instance. @@ -64,7 +64,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition $this->ogContext = $og_context; $this->ogMembership = $og_membership; - $this->user = $og_user; + $this->ogUser = $og_user; } /** @@ -139,7 +139,7 @@ public function getCacheTags() { * An array of groups, or an empty array if no group is found. */ protected function getCurrentUserGroupIds($entity_type = 'node') { - $groups = $this->ogMembership->getUserGroupIds($this->user); + $groups = $this->ogMembership->getUserGroupIds($this->ogUser); if (!empty($groups) && isset($groups[$entity_type])) { return $groups[$entity_type]; } From f6e0e52bee033464544b302653c4ba2b0214bbca Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Fri, 1 Dec 2017 11:19:50 +0000 Subject: [PATCH 3/4] Add missing getGroup method --- src/Plugin/views/argument_default/Membership.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/Plugin/views/argument_default/Membership.php b/src/Plugin/views/argument_default/Membership.php index b7e0c2038..d198f5d70 100644 --- a/src/Plugin/views/argument_default/Membership.php +++ b/src/Plugin/views/argument_default/Membership.php @@ -146,4 +146,19 @@ protected function getCurrentUserGroupIds($entity_type = 'node') { 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; + } + } + } + } From e9ea1564a94b7d59405c0542a4ff8caac5e923ef Mon Sep 17 00:00:00 2001 From: Dan Braghis Date: Wed, 26 Sep 2018 14:15:02 +0100 Subject: [PATCH 4/4] Tweak docblocks and comments --- src/Plugin/views/argument_default/Membership.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Plugin/views/argument_default/Membership.php b/src/Plugin/views/argument_default/Membership.php index d198f5d70..cfb7244a1 100644 --- a/src/Plugin/views/argument_default/Membership.php +++ b/src/Plugin/views/argument_default/Membership.php @@ -13,7 +13,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * Default argument plugin to provide the group from the current context. + * Default argument plugin to provide the group memberships from the current context. * * @ViewsArgumentDefault( * id = "og_group_membership", @@ -44,7 +44,7 @@ class Membership extends ArgumentDefaultPluginBase implements CacheableDependenc protected $ogUser; /** - * Constructs a new User instance. + * Constructs a new Membership instance. * * @param array $configuration * A configuration array containing information about the plugin instance. @@ -117,6 +117,11 @@ public function getCacheMaxAge() { * {@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']; } @@ -135,6 +140,9 @@ public function getCacheTags() { /** * Returns groups that current user is a member of. * + * @param string $entity_type + * The entity type, defaults to 'node'. + * * @return array * An array of groups, or an empty array if no group is found. */