You are here

FlagForListSubscriber.php in Flag Lists 4.0.x

Same filename and directory in other branches
  1. 8 src/EventSubscriber/FlagForListSubscriber.php

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;

/**
 * Subscribe to Flag events.
 */
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,
          ]));
        }
      }
    }
  }

}

Classes

Namesort descending Description
FlagForListSubscriber Subscribe to Flag events.