og-8.x-1.x-dev/tests/modules/og_test/src/EventSubscriber/OgTestEventSubscriber.php
tests/modules/og_test/src/EventSubscriber/OgTestEventSubscriber.php
<?php
declare(strict_types=1);
namespace Drupal\og_test\EventSubscriber;
use Drupal\Core\State\StateInterface;
use Drupal\og\Event\GroupContentEntityOperationAccessEventInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Event subscribers for testing Organic Groups.
*/
class OgTestEventSubscriber implements EventSubscriberInterface {
public function __construct(protected readonly StateInterface $state) {}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
GroupContentEntityOperationAccessEventInterface::EVENT_NAME => [['moderatorsCanManageComments']],
];
}
/**
* Allows moderators to edit and delete comments in all groups.
*
* @param \Drupal\og\Event\GroupContentEntityOperationAccessEventInterface $event
* The event that fires when an entity operation is being performed on group
* content.
*/
public function moderatorsCanManageComments(GroupContentEntityOperationAccessEventInterface $event): void {
if ($this->state->get('og_test_group_content_entity_operation_access_alter', FALSE)) {
// Moderators should have access to edit and delete all comments in all
// groups.
$is_comment = $event->getGroupContent()->getEntityTypeId() === 'comment';
$user_can_moderate_comments = $event->getUser()->hasPermission('edit and delete comments in all groups');
if ($is_comment && $user_can_moderate_comments) {
$event->grantAccess();
}
}
}
}
