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

Default argument for current user memberships. #253

Closed
wants to merge 4 commits into from
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 164 additions & 0 deletions src/Plugin/views/argument_default/Membership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php

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\views\Plugin\views\argument_default\ArgumentDefaultPluginBase;
use Drupal\og\MembershipManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Default argument plugin to provide the group from the current context.
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 User instance.
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
*
* @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 $og_user
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
* The user to be evaluated.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextProviderInterface $og_context, MembershipManagerInterface $og_membership, AccountInterface $og_user) {
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
parent::__construct($configuration, $plugin_id, $plugin_definition);

$this->ogContext = $og_context;
$this->ogMembership = $og_membership;
$this->ogUser = $og_user;
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@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'));
Copy link
Member

Choose a reason for hiding this comment

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

I think we could make it configurable - and expose the group related entities

Copy link
Author

Choose a reason for hiding this comment

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

That does sound a worthwhile aim.

I would likely need to return to this at a later date if I were to help with that though.

Copy link
Member

Choose a reason for hiding this comment

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

👍

}

/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return Cache::PERMANENT;
}

/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return ['og_role'];
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* {@inheritdoc}
*/
public function getCacheTags() {
$group = $this->getGroup();
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
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->ogUser);
MPParsley marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
}

}