chrzanek98/pimcore-object-event-listeners-bundle

This bundle lets create event listeners for pimcore DataObjects in easy and flexible way

1.0.0 2018-10-25 23:55 UTC

This package is auto-updated.

Last update: 2024-10-29 05:34:21 UTC


README

SymfonyInsight

Latest Stable Version Latest Unstable Version License

Installation

Step 1: Download the Bundle

Open a command console, enter your project directory and execute:

$ composer require chrzanek98/pimcore-object-event-listeners-bundle

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new \Chrzanek98\PimcoreObjectEventListenersBundle\Chrzanek98PimcoreObjectEventListenersBundle(),
        );

        // ...
    }

    // ...
}

How it works?

Step 1

Create handler class e.g. AcmeEventListener.php and extend it with AbstractHandler

<?php

namespace AppBundle\EventListeners;

use Chrzanek98\PimcoreObjectEventListenersBundle\EventListeners\Providers\AbstractHandler;
use Pimcore\Event\Model\DataObjectEvent;

class AcmeEventListener extends AbstractHandler
{
    // ...
}

Step 2

Implement canHandle method

<?php

namespace AppBundle\EventListeners;

// ...

class AcmeEventListener extends AbstractHandler
{
    public function canHandle(DataObjectEvent $element)
    {
        return $element->getObject() instanceof Acme;
    }
}

Step 3

Use desired hook, currently available are pre/post Add/Update/Delete

<?php

namespace AppBundle\EventListeners;

// ...

class AcmeEventListener extends AbstractHandler
{
    public function preUpdate(DataObjectEvent $element)
    {
        throw new NotFoundHttpException('Lorem ipsum dolor sit amet');
    }

    public function canHandle(DataObjectEvent $element)
    {
        return $element->getObject() instanceof Acme;
    }
}

Step 4

Register your event handler as a service with tag object.handler

    object.handler.acme:
        class: AppBundle\EventListeners\AcmeEventListener
        tags:
            - { name: object.handler }