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 — how to run collection id if statement for control panel event

Hi,

I'm new to working with APIs so struggling a little, but I've been reading through the docs.

What I would like to do is fire off an event when a certain collection is saved. I'm half way there, so I have something like this

$API->on('collection.publish_item', function(PerchSystemEvent $Event){

However, is it possible to only fire off an event when a certain collection name is saved? something like this:

$API->on('collection.publish_item', function(PerchSystemEvent $Event){
    if collection_id = x {

    }

Some other questions I have:

  • If this is possible, is there some documentation listing what variables I can test for? If not, how do I find out the Perch variables available?
  • how do I find out the collection id?
Jay George

Jay George 2 points

  • 1 year ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

If you inspect the $Event->subject property, you should have the collection.

Take a look here: https://docs.grabaperch.com/api/events/ it lists the properties of the Event that you can access in your code.

When you say inspect, do you mean do a var dump?

If I do a var dump, I can see that the CollectionID is shown but under Protected details e.g.

protected 'details' => 
        array (size=13)
          'itemRowID' => string '1587' (length=4)
          'itemID' => string '113' (length=3)
          'itemRev' => string '36' (length=2)
          'collectionID' => string '1' (length=1)

Does this mean it's not available? If I try to echo out collectionID nothing appears e.g. echo $Event->collectionID;

Drew McLellan

Drew McLellan 2638 points
Perch Support

$Collection = $Event->subject;
$collectionID = $Collection->id();

Thanks, that gave me a clue to how to get there.

To get the variable I had to do…

$API->on('collection.publish_item', function(PerchSystemEvent $Event){
        $Collection = $Event->subject;
        $collectionID = $Collection->CollectionID();

        echo $collectionID; <--then manipulate this
});