You are here

class OgTestEventSubscriber in Organic groups 8

Event subscribers for testing Organic Groups.

Hierarchy

  • class \Drupal\og_test\EventSubscriber\OgTestEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OgTestEventSubscriber

1 string reference to 'OgTestEventSubscriber'
og_test.services.yml in tests/modules/og_test/og_test.services.yml
tests/modules/og_test/og_test.services.yml
1 service uses OgTestEventSubscriber
og_test.event_subscriber in tests/modules/og_test/og_test.services.yml
Drupal\og_test\EventSubscriber\OgTestEventSubscriber

File

tests/modules/og_test/src/EventSubscriber/OgTestEventSubscriber.php, line 14

Namespace

Drupal\og_test\EventSubscriber
View source
class OgTestEventSubscriber implements EventSubscriberInterface {

  /**
   * The state service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * Constructs an OgTestEventSubscriber.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service.
   */
  public function __construct(StateInterface $state) {
    $this->state = $state;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    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();
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgTestEventSubscriber::$state protected property The state service.
OgTestEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OgTestEventSubscriber::moderatorsCanManageComments public function Allows moderators to edit and delete comments in all groups.
OgTestEventSubscriber::__construct public function Constructs an OgTestEventSubscriber.