Listen to events

Edit on GitHub

There are two ways to listen to events: using direct listeners or subscribers. The difference between these two is that a subscriber allows the module providing the subscriber to wire up the handlers in the module that owns it without touching the EventDependencyProvider exception’s initial subscriber initialization. It is best to use a subscriber from the beginning, as this simplifies future listener registration. When you need to listen to specific listener, use \Pyz\Zed\Event\EventDependencyProvider::getEventListenerCollection.

Listen to events with direct listeners

To listen to events in the system, you need to add listener plugins. A listener is a class that extends plugins and implements EventListenerInterface. This plugin must be placed in the module you want to listen to: Communication\Plugin\Product\ProductRelationAbstractProductChangeListener.

For example, each listener plugin must implement \Spryker\Service\Event\Dependency\Plugin\EventListenerInterface.

<?php
namespace Spryker\Zed\ProductRelation\Communication\Plugin\Product;

use Spryker\Zed\Event\Dependency\Plugin\EventListenerInterface;
use Spryker\Shared\Kernel\Transfer\TransferInterface;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;

/**
 * @method \Spryker\Zed\ProductRelation\Business\ProductRelationFacade getFacade()
 * @method \Spryker\Zed\ProductRelation\Communication\ProductRelationCommunicationFactory getFactory()
 */
class ProductRelationAbstractProductChangeListener extends AbstractPlugin implements EventListenerInterface
{

    /**
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface|\Generated\Shared\Transfer\ProductAbstractTransfer $eventTransfer
     *
     * @return void
     */
    public function handle(TransferInterface $eventTransfer)
    {
        $this->getFacade()->applyRulesToProductAbstract($eventTransfer);
    }

}

Add this listener to \Pyz\Zed\Event\EventDependencyProvider::getEventListenerCollection

Example:

$eventCollection->addListener(ProductEvents::PRODUCT_ABSTRACT_BEFORE_CREATE, new ProductRelationAbstractProductChangeListener())`

Listen to events with a subscriber

Event subscribers are another way to listen to events in the system. The advantage of using event subscribers is that all the listeners reside in the module you want to react to the event. Register a subscriber once in EventDependencyProvider and later a listener can be added without touching other modules.

To implement a subscriber, follow these steps:

  1. Create an event subscriber class. Example:
<?php
namespace Spryker\Zed\ProductRelation\Communication\Plugin;

use Spryker\Zed\Event\Dependency\EventCollectionInterface;
use Spryker\Zed\Event\Dependency\Plugin\EventSubscriberInterface;
use Spryker\Zed\Product\Dependency\ProductEvents;
 Spryker\Zed\ProductRelation\Communication\Plugin\Product\ProductRelationAbstractProductChangeListener;

use Spryker\Zed\Kernel\Communication\AbstractPlugin;

/**
 * Sample implementation for subscriber
 */
class ProductRelationEventSubscriber extends AbstractPlugin implements EventSubscriberInterface
{

    /**
     * @param \Spryker\Zed\Event\Dependency\EventCollectionInterface $eventCollection
     *
     * @return \Spryker\Zed\Event\Dependency\EventCollectionInterface
     */
    public function getSubscribedEvents(EventCollectionInterface $eventCollection)
    {
        return $eventCollection
            ->addListener(ProductEvents::PRODUCT_ABSTRACT_BEFORE_CREATE, new ProductRelationAbstractProductChangeListener());

    }

}
  1. Include this subscriber class to \Pyz\Zed\Event\EventDependencyProvider::getEventSubscriberCollection.
<?php

/**
 * @return \Spryker\Zed\Event\Dependency\EventSubscriberCollectionInterface
 */
public function getEventSubscriberCollection()
{
    $eventSubscriberCollection = new EventSubscriberCollection();
    $eventSubscriberCollection->add(new ProductRelationEventSubscriber());

    return $eventSubscriberCollection
}