OgTestEventSubscriber.php in Organic groups 8
File
tests/modules/og_test/src/EventSubscriber/OgTestEventSubscriber.php
View source
<?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;
class OgTestEventSubscriber implements EventSubscriberInterface {
protected $state;
public function __construct(StateInterface $state) {
$this->state = $state;
}
public static function getSubscribedEvents() {
return [
GroupContentEntityOperationAccessEventInterface::EVENT_NAME => [
[
'moderatorsCanManageComments',
],
],
];
}
public function moderatorsCanManageComments(GroupContentEntityOperationAccessEventInterface $event) : void {
if ($this->state
->get('og_test_group_content_entity_operation_access_alter', FALSE)) {
$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();
}
}
}
}