FlagEvents.php in Flag 8.4
File
tests/modules/flag_event_test/src/EventSubscriber/FlagEvents.php
View source
<?php
namespace Drupal\flag_event_test\EventSubscriber;
use Drupal\Core\State\StateInterface;
use Drupal\flag\Event\FlaggingEvent;
use Drupal\flag\Event\UnflaggingEvent;
use Drupal\flag\FlagServiceInterface;
use Drupal\flag\Event\FlagEvents as Flag;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class FlagEvents implements EventSubscriberInterface {
protected $flagService;
protected $state;
public function __construct(FlagServiceInterface $flag_service, StateInterface $state) {
$this->flagService = $flag_service;
$this->state = $state;
}
public static function getSubscribedEvents() {
$events[Flag::ENTITY_FLAGGED] = [
'onFlag',
50,
];
$events[Flag::ENTITY_UNFLAGGED] = [
'onUnflag',
50,
];
return $events;
}
public function onFlag(FlaggingEvent $event) {
if ($flag_id = $this->state
->get('flag_test.react_flag_event', FALSE)) {
$flag = $this->flagService
->getFlagById($flag_id);
assert('$event->getFlagging()->getFlag()->id() !== $flag->id()', 'Should not test the flagging event with the same flag that is being flagged.');
$this->state
->set('flag_test.is_flagged', $flag
->isFlagged($event
->getFlagging()
->getFlaggable(), $event
->getFlagging()
->getOwner()));
}
}
public function onUnflag(UnflaggingEvent $event) {
if ($flag_id = $this->state
->get('flag_test.react_unflag_event', FALSE)) {
$flag = $this->flagService
->getFlagById($flag_id);
foreach ($event
->getFlaggings() as $flagging) {
assert('$flagging->getFlag()->id() != $flag->id()', 'Should not test the unflagging event with the same flag that is being unflagged.');
$this->state
->set('flag_test.is_unflagged', $flag
->isFlagged($flagging
->getFlaggable(), $flagging
->getOwner()));
}
}
}
}