Action Interceptors

Hi there!
I want to know if there is any kind of magic or something like that let me intercept actions on admin, eg Save, Delete, Publish and how can I implement more functions on them, like Publishing on a Facebook page everytime I publish a new post on the site.

1 Like

It’s called events. The are available in the Java node api. This is for instance used to update the content UI when data is updated.

Ok. But what I want to know is if there is any way to catch these events on the Javascript side. Maybe creating a lib, an admin tool or something else…

Currently no, but you can create a lib with a small piece if Java to catch the event and then trigget a script I guess? We are working on a “task” api where javascripts can be launched like this in a simple fashion. Maybe @srs knows more about your usecase?

1 Like

+1 on this one :wink:

Is this in the 6.8 (or 6.9) release that comes with the Node API?

Hi, as Thomas says, its simple to create an event-listener:

import org.osgi.service.component.annotations.Component;
import com.enonic.xp.event.Event;
import com.enonic.xp.event.EventListener;

@Component(immediate = true)
public class MyEventListener
    implements EventListener
{

    @Override
    public void onEvent( final Event event )
    {
        System.out.println( "Event received: " + event.getType() );
    }
}

Then, you could use websockets to send / receive the events on the js-side. Have a look at https://github.com/enonic/xp/tree/master/modules/admin/admin-event/src/main/java/com/enonic/xp/admin/event/impl package and websocket-lib for reference.

3 Likes

As things look right now, a javascript event lib will be made available with 6.9 - so you can intercept and process events even in Javascript :slight_smile:

5 Likes

I see that the event-lib is already on the v6.9.0-SNAPSHOT hehe. How can I play with it for now? I mean, it will be used like the jobs-lib, so we’ll have a folder with files “events/[eventType].js”?

It’s in 6.9.0 snapshot and will be there in the final release. Not sure what you mean by the “jobs-lib”. Is that something that you have developed or are you referring something else?

You can add event listeners anywhere by adding the lib-event to your project and start using it like any other libs (read the reference).

eventLib.listener({
    type: 'node.*',
    localOnly: false,
    callback: function (event) {
        log.info(JSON.stringify(event));
    }
});

The above example will add a listener on all node events (locally only to this cluster node) and just print out the event.

The event listeners can be placed anywhere, but if you want to add them when you initialize your app, you will need to create a main.js file in your app (see docs here).

Hope this helps.

When I said “jobs-lib” I mean the CronJob app in the market :stuck_out_tongue:
Anyway, I understood how the listener could be used. Thanks ^^

Oh, right. We where thinking about to use the same pattern as tasks, pages, parts etc. But found out that listeners is much more fine-grained. So it fits better in the startup-sequence of the app.

1 Like

Nice! Could you place the expected return on this page, like you did for other older functions? Just to know what we should expect xD
If it’s anyone interest, the event object is like:
{ "type": "node.updated", "timestamp": 1482940084451, "localOrigin": true, "distributed": true, "data": { "nodes": "[{id=[_id or principalKey], path=[_path], branch=draft, repo=cms-repo}]" } }

1 Like

Yes, will do that. The return object depends a little on the “payload” of the event, but the structure except “data” is similar.

1 Like