Collection item index event

Sometimes it makes sense to perform some automated processes when a Collection item is saved. You may want to generate a sitemap for a very large Collection, or perhaps generate a dynamic social sharing image for the updated item.

In Perch v3, both the region item index event and the collection item index event are called region.index:

$API  = new PerchAPI(1.0, 'my_app');

$API->on('region.index', function(PerchSystemEvent $Event) {
    // content region item or collection item was saved
});

One way to determine what the event was is to check for the class name of the event subject:

$API->on('region.index', function(PerchSystemEvent $Event) {
    if(get_class($Event->subject) == 'PerchContent_Region') {
        // content region item was saved
    }

    if(get_class($Event->subject) == 'PerchContent_CollectionItem') {
        // collection item was saved
    }
});

Another way is to listen to collection.publish_item first, then listen to region.index:

$API->on('collection.publish_item', function(PerchSystemEvent $Event) use($API){
    // collection item saved, index not updated yet

    $API->on('region.index', function(PerchSystemEvent $Event) {
        // collection item index updated
    });
});

Perch does a lot under the hood when saving a collection item and it fires the collection.publish_item event at a stage before updating the item index. Later when it’s done updating the item index, it fires region.index.

link

Related articles