You are here

class FlagSearchApiSubscriber in Flag Search API 8

Class FlagSearchApiSubscriber.

Hierarchy

  • class \Drupal\flag_search_api\EventSubscriber\FlagSearchApiSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of FlagSearchApiSubscriber

1 string reference to 'FlagSearchApiSubscriber'
flag_search_api.services.yml in ./flag_search_api.services.yml
flag_search_api.services.yml
1 service uses FlagSearchApiSubscriber
flag_search_api.event_subscriber in ./flag_search_api.services.yml
Drupal\flag_search_api\EventSubscriber\FlagSearchApiSubscriber

File

src/EventSubscriber/FlagSearchApiSubscriber.php, line 13

Namespace

Drupal\flag_search_api\EventSubscriber
View source
class FlagSearchApiSubscriber implements EventSubscriberInterface {

  /**
   * Drupal\Core\Entity\EntityTypeManagerInterface definition.
   *
   * @var \Drupal\flag_search_api\FlagSearchApiReindexService
   */
  protected $flagSearchApiReindex;

  /**
   * Constructs a new FlagSearchApiSubscriber object.
   *
   * @param \Drupal\flag_search_api\FlagSearchApiReindexService $flag_search_api_reindex_service
   *   FlagSearchApiReindexService.
   */
  public function __construct(FlagSearchApiReindexService $flag_search_api_reindex_service) {
    $this->flagSearchApiReindex = $flag_search_api_reindex_service;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events['flag.entity_flagged'] = [
      'flagEntityFlagged',
    ];
    $events['flag.entity_unflagged'] = [
      'flagEntityUnflagged',
    ];
    return $events;
  }

  /**
   * Method is called whenever the flag.entity_flagged event is dispatched.
   *
   * @param \Drupal\flag\Event\FlaggingEvent $event
   *   Event.
   */
  public function flagEntityFlagged(FlaggingEvent $event) {
    $this->flagSearchApiReindex
      ->reindexItem($event
      ->getFlagging());
  }

  /**
   * Method is called whenever the flag.entity_unflagged event is dispatched.
   *
   * @param \Drupal\flag\Event\UnflaggingEvent $event
   *   Event.
   */
  public function flagEntityUnflagged(UnflaggingEvent $event) {
    $flaggings = $event
      ->getFlaggings();

    /** @var \Drupal\flag\FlaggingInterface $flagging */
    foreach ($flaggings as $flagging) {
      $this->flagSearchApiReindex
        ->reindexItem($flagging);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagSearchApiSubscriber::$flagSearchApiReindex protected property Drupal\Core\Entity\EntityTypeManagerInterface definition.
FlagSearchApiSubscriber::flagEntityFlagged public function Method is called whenever the flag.entity_flagged event is dispatched.
FlagSearchApiSubscriber::flagEntityUnflagged public function Method is called whenever the flag.entity_unflagged event is dispatched.
FlagSearchApiSubscriber::getSubscribedEvents public static function
FlagSearchApiSubscriber::__construct public function Constructs a new FlagSearchApiSubscriber object.