You are here

class FlagForListSubscriber in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/EventSubscriber/FlagForListSubscriber.php \Drupal\flag_lists\EventSubscriber\FlagForListSubscriber

Class FlagForListSubscriber.

Hierarchy

Expanded class hierarchy of FlagForListSubscriber

1 string reference to 'FlagForListSubscriber'
flag_lists.services.yml in ./flag_lists.services.yml
flag_lists.services.yml
1 service uses FlagForListSubscriber
flag_lists.event_subscriber_flag in ./flag_lists.services.yml
Drupal\flag_lists\EventSubscriber\FlagForListSubscriber

File

src/EventSubscriber/FlagForListSubscriber.php, line 13

Namespace

Drupal\flag_lists\EventSubscriber
View source
class FlagForListSubscriber implements EventSubscriberInterface {
  use MessengerTrait;

  /**
   * Constructs a new FlagForListSubscriber object.
   */
  public function __construct() {
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[FlagEvents::ENTITY_FLAGGED][] = [
      'flagListsEntityFlagged',
      100,
    ];
    $events[FlagEvents::ENTITY_UNFLAGGED][] = [
      'flagListsEntityUnflagged',
      100,
    ];
    return $events;
  }

  /**
   * This is called whenever the flag.entity_flagged event is dispatched.
   *
   * @param Symfony\Component\EventDispatcher\Event $flag_event
   *   The response event.
   */
  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,
        ]));
      }
    }
  }

  /**
   * This is called whenever the flag.entity_unflagged event is dispatched.
   *
   * @param Symfony\Component\EventDispatcher\Event $flag_events
   *   The response events.
   */
  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,
          ]));
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagForListSubscriber::flagListsEntityFlagged public function This is called whenever the flag.entity_flagged event is dispatched.
FlagForListSubscriber::flagListsEntityUnflagged public function This is called whenever the flag.entity_unflagged event is dispatched.
FlagForListSubscriber::getSubscribedEvents public static function
FlagForListSubscriber::__construct public function Constructs a new FlagForListSubscriber object.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.