og-8.x-1.x-dev/src/Plugin/OgGroupResolver/RequestQueryArgumentResolver.php
src/Plugin/OgGroupResolver/RequestQueryArgumentResolver.php
<?php
declare(strict_types=1);
namespace Drupal\og\Plugin\OgGroupResolver;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\og\Attribute\OgGroupResolver;
use Drupal\og\GroupTypeManagerInterface;
use Drupal\og\OgGroupResolverBase;
use Drupal\og\OgResolvedGroupCollectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Resolves the group from the query arguments on the request.
*
* This plugin inspects the current request and checks if there are query
* arguments available that point to a group entity.
*/
#[OgGroupResolver(
id: 'request_query_argument',
label: new TranslatableMarkup('Group entity from query arguments'),
description: new TranslatableMarkup('Checks if the current request has query arguments that indicate the group context.')
)]
class RequestQueryArgumentResolver extends OgGroupResolverBase implements ContainerFactoryPluginInterface {
/**
* The query argument that holds the group entity type.
*/
const GROUP_TYPE_ARGUMENT = 'og-type';
/**
* The query argument that holds the group entity ID.
*/
const GROUP_ID_ARGUMENT = 'og-id';
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected readonly RequestStack $requestStack,
protected readonly GroupTypeManagerInterface $groupTypeManager,
protected readonly EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('request_stack'),
$container->get('og.group_type_manager'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function resolve(OgResolvedGroupCollectionInterface $collection) {
// Check if our arguments are present on the request.
$query = $this->requestStack->getCurrentRequest()->query;
if ($query->has(self::GROUP_TYPE_ARGUMENT) && $query->has(self::GROUP_ID_ARGUMENT)) {
try {
$storage = $this->entityTypeManager->getStorage($query->get(self::GROUP_TYPE_ARGUMENT));
}
catch (InvalidPluginDefinitionException $e) {
// Invalid entity type specified, cannot resolve group.
return;
}
// Load the entity and check if it is a group.
/** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
if ($entity = $storage->load($query->get(self::GROUP_ID_ARGUMENT))) {
if ($this->groupTypeManager->isGroup($entity->getEntityTypeId(), $entity->bundle())) {
// Only add a vote for the group if it already has been discovered by
// a previous plugin. This will make sure that users cannot fake a
// group context by messing with the query arguments.
if ($collection->hasGroup($entity)) {
$collection->addGroup($entity, ['url']);
}
}
}
}
}
}
