We are transitioning over to a new forum platform. Please post new issues there. Existing threads will be dealt with here unless you choose to move them over. Visit the new forum

Forum

Thread tagged as: Question, Api

Perch API — delete item in collection

Hi,

Is there a way to detect when an item is deleted from a collection using the API? It's not listed in the docs -https://docs.grabaperch.com/api/events/list/

I did actually try $API->on('collection.delete_item', function(PerchSystemEvent $Event){ just in case it was there but not documented, but no cigar.

I need this event to communicate with the Algolia API, deleting a record when Perch does.

Jay George

Jay George 2 points

  • 1 year ago
Duncan Revell

Duncan Revell 78 points
Registered Developer

Maybe try item.delete - it looks like it might work, but I haven't tried.

I guess it will pick other deletes, so you'll have to code to find out if it comes from a collection or a region etc.

Thanks,

I suppose I could do something like…

$API->on('item.delete', function(PerchSystemEvent $Event){

        // Get the collection ID
        $collectionID = $Event->subject->CollectionID();

        // Only do something if we're in a particular collection.
        if($collectionID == 1) {

Update: For some reason I'm not able to query the collectionID using the delete item event i.e. if($collectionID == somenumber) { as above does not trigger anything.

I have realised that it's safe to perform an event on $Event->subject->itemID(); though, since it's unique to the current record, so there's no chance of me accidentally affecting other items.

Duncan Revell

Duncan Revell 78 points
Registered Developer

Yeah, I think that's what you need to watch out - you can't guarantee you're getting a Collection item. It could be a Region item - or as you say, just an Item. If you var dump it, does it give you anything to do with where the item has come from?

If you var dump it, does it give you anything to do with where the item has come from?

No, it doesn't. I see things with a var dump on a different event like $API->on('collection.publish_item', function(PerchSystemEvent $Event){, but not on delete. I think maybe because of the confirmation screen? it seems to be more of a "dumb" operation.

I'm curious if Drew has any more information, although personally I've realised my problem is solved because of the unique ID of the deleted record.

Drew McLellan

Drew McLellan 2638 points
Perch Support

The event subject will either be a PerchContent_CollectionItem or a PerchContent_Item.

get_class() will tell you which.