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

254 Default argument membership #564

Merged
Changes from all 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
172 changes: 172 additions & 0 deletions src/Plugin/views/argument_default/Membership.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?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 memberships from the current context.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
* Default argument plugin to provide the group memberships from the current context.
* Default argument plugin to provide 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 $og_user
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
* @param \Drupal\Core\Session\AccountInterface $og_user
* @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 $og_user) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContextProviderInterface $og_context, MembershipManagerInterface $og_membership, AccountInterface $og_user) {
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 = $og_user;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
$this->ogUser = $og_user;
$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' => ''];

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() {
// This cache context is the best thing we have right now.
// og_role takes in consideration the user memberships and
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
// og_role takes in consideration the user memberships and
// og_role takes in consideration the user memberships and

// the roles held in the corresponding groups, and while it
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
// the roles held in the corresponding groups, and while it
// the roles held in the corresponding groups, and while it

// is one level too granular, i.e. the context will be more
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
// is one level too granular, i.e. the context will be more
// 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.
*
* @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.
*/
protected function getCurrentUserGroupIds($entity_type = 'node') {
$groups = $this->ogMembership->getUserGroupIds($this->ogUser);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Suggested change
$groups = $this->ogMembership->getUserGroupIds($this->ogUser);
$groups = $this->ogMembership->getUserGroupIds($this->ogUser->id());

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;
}
}
}

}