class FlagEvents in Flag 8.4
Same name in this branch
- 8.4 src/Event/FlagEvents.php \Drupal\flag\Event\FlagEvents
- 8.4 tests/modules/flag_event_test/src/EventSubscriber/FlagEvents.php \Drupal\flag_event_test\EventSubscriber\FlagEvents
Test flag events subscriber.
Hierarchy
- class \Drupal\flag_event_test\EventSubscriber\FlagEvents implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of FlagEvents
1 string reference to 'FlagEvents'
- flag_event_test.services.yml in tests/
modules/ flag_event_test/ flag_event_test.services.yml - tests/modules/flag_event_test/flag_event_test.services.yml
1 service uses FlagEvents
- flag_event_test.flag_subscriber in tests/
modules/ flag_event_test/ flag_event_test.services.yml - \Drupal\flag_event_test\EventSubscriber\FlagEvents
File
- tests/
modules/ flag_event_test/ src/ EventSubscriber/ FlagEvents.php, line 15
Namespace
Drupal\flag_event_test\EventSubscriberView source
class FlagEvents implements EventSubscriberInterface {
/**
* The flag service.
*
* @var \Drupal\flag\FlagServiceInterface
*/
protected $flagService;
/**
* The state service.
*
* @var \Drupal\Core\State\StateInterface
*/
protected $state;
/**
* Constructor.
*
* @param \Drupal\flag\FlagServiceInterface $flag_service
* The flag service.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
*/
public function __construct(FlagServiceInterface $flag_service, StateInterface $state) {
$this->flagService = $flag_service;
$this->state = $state;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[Flag::ENTITY_FLAGGED] = [
'onFlag',
50,
];
$events[Flag::ENTITY_UNFLAGGED] = [
'onUnflag',
50,
];
return $events;
}
/**
* React to flagging event.
*
* @param \Drupal\flag\Event\FlaggingEvent $event
* The flagging event.
*/
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()));
}
}
/**
* React to unflagging event.
*
* @param \Drupal\flag\Event\UnflaggingEvent $event
* The unflagging event.
*/
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()));
}
}
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FlagEvents:: |
protected | property | The flag service. | |
FlagEvents:: |
protected | property | The state service. | |
FlagEvents:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
FlagEvents:: |
public | function | React to flagging event. | |
FlagEvents:: |
public | function | React to unflagging event. | |
FlagEvents:: |
public | function | Constructor. |