Edit on GitHub

Events and Event Listeners

General

Pimcore provides an extensive number of events that are fired during execution of Pimcore functions. These events can be used to hook into many Pimcore functions such as saving an object, asset or document and can be used to change or extend the default behavior of Pimcore.

The most common use-case for events is using them in a bundle/extension, but of course you can use them also anywhere in your code or in your dependency injection configuration (app/config/services.yml).

Pimcore implements the standard Symfony framework event dispatcher and just adds some pimcore specific events, so you can also subscribe to all Symfony core eventsand events triggered by arbitrary Symfony bundles.

For that reason it's recommended to have a look into the Symfony Events and Event Listeners documentation first, which covers all basics in that matter.

Available Events

All Pimcore events are defined and documented as a constant on component specific classes:

Examples

Hook into the pre-update event of assets, documents and objects

The following example shows how to register events for assets, documents and objects

in your app/config/services.yml:

services:
    AppBundle\EventListener\TestListener:
        tags:
            - { name: kernel.event_listener, event: pimcore.asset.preUpdate, method: onPreUpdate }
            - { name: kernel.event_listener, event: pimcore.document.preUpdate, method: onPreUpdate }
            - { name: kernel.event_listener, event: pimcore.dataobject.preUpdate, method: onPreUpdate }

in your listener class src/AppBundle/EventListener/TestListener

<?php
namespace AppBundle\EventListener;
  
use Pimcore\Event\Model\ElementEventInterface;
use Pimcore\Event\Model\DataObjectEvent;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Event\Model\DocumentEvent;

class TestListener {
     
    public function onPreUpdate (ElementEventInterface $e) {
       
        if($e instanceof AssetEvent) {
            // do something with the asset
            $foo = $e->getAsset(); 
        } else if ($e instanceof DocumentEvent) {
            // do something with the document
            $foo = $e->getDocument(); 
        } else if ($e instanceof DataObjectEvent) {
            // do something with the object
            $foo = $e->getObject(); 
            $foo->setMyValue(microtime(true));
            // we don't have to call save here as we are in the pre-update event anyway ;-) 
        }
    }
}

Hook into the list of objects in the tree, the grid list and the search panel

There are some global events having effect on several places. One of those is pimcore.admin.object.list.beforeListLoad. The object list can be modified (changing condition for instance) before being loaded. This global event will apply to the tree, the grid list, the search panel and everywhere objects are listed in the Pimcore GUI. This way, it is possible to create custom permission rules to decide if the object should or not be listed (for instance, the user must be the owner of the object, ...).

It extends the possibility further than just limiting list permissions folder by folder depending the user/role object workspace. To ensure maximum security, it is advisable to combine this with an object DI to overload isAllowed method with the same custom permission rules. This way proper permission is ensuring all the way (rest services, ...).