Message-ID: <1048226895.2776.1485850764566.JavaMail.confluence@ip-10-127-227-164> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_2775_222288487.1485850764566" ------=_Part_2775_222288487.1485850764566 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html Listening to Core events

Listening to Core events

=20
=20
=20
=20

Description

When you interact with the Public API, and with the content Repository i= n particular, Signals may be sent out, allowing you to rea= ct on actions triggered by the Repository. Those signals can be received by= dedicated services called Slots.

To learn more about SignalSlot in eZ Platform, please refer to the = dedicated documentation page.

Si= gnals reference

This recipe will describe how to register a Slot for a dedicated Signal.=

Solution

Registerin= g a Slot for a given Signal

As described in the SignalSlot documentation, a Slot is roughly like an event = listener and must extend eZ\Publish\Core\SignalSlot\Slot.=

A typical implementation is the following:

OnPublishSlot
=20
namespace Acme\TestBundle\Slot;
 
use eZ\Publish\Core\SignalSlot\Slot as BaseSlot;
use eZ\Publish\Core\SignalSlot\Signal;
use eZ\Publish\API\Repository\ContentService;
=20
class OnPublishSlot extends BaseSlot
{
    /**
     * @var \eZ\Publish\API\Repository\ContentService
     */
    private $contentService;

    public function __construct( ContentService $contentService )
    {
        $this->contentService =3D $contentService;
    }
=20
    public function receive( Signal $signal )
    {
        if ( !$signal instanceof Signal\ContentService\PublishVersionSignal=
 )
        {
            return;
        }

        // Load published content
        $content =3D $this->contentService->loadContent( $signal->=
contentId, null, $signal->versionNo );
        // Do stuff with it...
    }
}
=20

OnPublishSlot now needs to be registered as a service in th= e ServiceContainer and identified as a valid Slot:

services.yml (in your bundle)
=20
parameters:
    my.onpublish_slot.class: Acme\TestBundle\Slot\OnPublishSlot
 
services:
    my.onpublish_slot:
        class: %my.onpublish_slot.class%
        arguments: [@ezpublish.api.service.content]
        tags:
            - { name: ezpublish.api.slot, signal: ContentService\PublishVer=
sionSignal }
=20

Service tag ezpublish.api.slot identifies = your service as a valid Slot. The signal part (mandatory) says that this sl= ot is listening to ContentService\PublishVersionSignal= (shortcut for \eZ\Publish\Core\SignalSlot\Signal\ContentSer= vice\PublishVersionSignal).

Internal signals emitted by Repository services are always relative to <= code>eZ\Publish\Core\SignalSlot\Signal namespace.

Hence ContentService\PublishVersionSignal means eZ\Pu= blish\Core\SignalSlot\Signal\ContentService\PublishVersionSignal.

Tip

You can register a slot for any kind of signal by setting signal to * in the service tag.

Using a ba= sic Symfony event listener

eZ Platform comes with a generic slot that converts signals (including o= nes defined by user code) to regular event objects and exposes them via the= EventDispatcher. This makes it possible to implement a simple event listen= er/subscriber if you're more comfortable with this approach.

All you need to do is to implement an event listener or subscriber and r= egister it.

Example

This very simple example will just log the received signal.

services.yml (in your bundle)
=20
parameters:
    my.signal_listener.class: Acme\TestBundle\EventListener\SignalListener
 
services:
    my.signal_listener:
        class: %my.signal_listener.class%
        arguments: [@logger]
        tags:
            - { name: kernel.event_subscriber }
=20
=20
<?php
namespace Acme\TestBundle\EventListener;

use eZ\Publish\Core\MVC\Symfony\Event\SignalEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class SignalListener implements EventSubscriberInterface
{
    /**
     * @var \Psr\Log\LoggerInterface
     */
    private $logger;

    public function __construct( LoggerInterface $logger )
    {
        $this->logger =3D $logger;
    }

    public function onAPISignal( SignalEvent $event )
    {
        $signal =3D $event->getSignal();
        // You may want to check the signal type here to react accordingly
        $this->logger->debug( 'Received Signal: ' . print_r( $signal,=
 true ) );
    }

    public static function getSubscribedEvents()
    {
        return array(
            MVCEvents::API_SIGNAL =3D> 'onAPISignal'
        );
    }
}
=20
=20
=20
=20
=20

In this topic:

Related topics:

Events

Signal-Slot

Si= gnals reference

=20
=20
=20
------=_Part_2775_222288487.1485850764566--