FlagForListSubscriber.php in Flag Lists 4.0.x
File
src/EventSubscriber/FlagForListSubscriber.php
View source
<?php
namespace Drupal\flag_lists\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\Event;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\flag\Event\FlagEvents;
class FlagForListSubscriber implements EventSubscriberInterface {
use MessengerTrait;
public function __construct() {
}
public static function getSubscribedEvents() {
$events[FlagEvents::ENTITY_FLAGGED][] = [
'flagListsEntityFlagged',
100,
];
$events[FlagEvents::ENTITY_UNFLAGGED][] = [
'flagListsEntityUnflagged',
100,
];
return $events;
}
public function flagListsEntityFlagged(Event $flag_event) {
$flagId = $flag_event
->getFlagging()
->getFlagId();
foreach (\Drupal::service('flaglists')
->getAllFlagForList() as $collection) {
if ($collection
->getBaseFlag() == $flagId) {
$this
->messenger()
->addMessage(t('Entity added to the %flaglist list.', [
'%flaglist' => $flagId,
]));
}
}
}
public function flagListsEntityUnflagged(Event $flag_events) {
foreach ($flag_events
->getFlaggings() as $flagging) {
$flagId = $flagging
->getFlagId();
foreach (\Drupal::service('flaglists')
->getAllFlagForList() as $collection) {
if ($collection
->getBaseFlag() == $flagId) {
$this
->messenger()
->addMessage(t('Entity removed from the %flaglist list.', [
'%flaglist' => $flagId,
]));
}
}
}
}
}