Page events
Intro
Xalok allows you to register event listeners that can execute some code when an article (or any other Page
entity, be it boards or listings) is published or unpublished.
These events come in two flavours, a synchronous version and a background one. The background ones are executed as part of Resque (for v4-v5) or Symfony Messenger (for v6+) in the background - at the same time that Xalok sends an invalidation to Varnish cache, for example.
Especially if you're doing I/O operations in this code (e.g.: contacting external services), you should consider using the background event listeners. These have the advantage that if the I/O operation fails (e.g.: the external service is down) or is very slow, the editor doesn't have to wait until the operation ends - the code will be executed in a separate (background) process, so the editor can move on with his work after hitting publish/unpublish buttons.
Using the events
Create a Symfony service and tag it accordingly:
<service id="app.my_wonderful_page_publish_listener" class="App\Bundle\CmsAdminBundle\Listeners\MyWonderfulPagePublishListener">
<!-- ... inject services needed by your service -->
<tag name="kernel.event_listener" event="__EVENT_NAME__" method="nameOfTheMethodHandlingTheEvent" />
</service>
Suggestion: by convention the nameOfTheMethodHandlingTheEvent
should be onPublish
or onUnpublish
, or something along these lines.
The method that you use for tagging the event will be invoked with a Wf\Bundle\CmsBaseAdminBundle\Event\PagePublishEvent
object:
<?php
namespace App\Bundle\CmsAdminBundle\Listeners;
use Wf\Bundle\CmsBaseAdminBundle\Event\PagePublishEvent;
class MyWonderfulPagePublishListener
{
public function nameOfTheMethodHandlingTheEvent(PagePublishEvent $event)
{
$page = $event->getPage();
}
}
Page events
These are triggered when any sort of Page
entity is being published or not (articles, boards, listings, etc.). You can use if ($entity instanceof PageBoard)
checks to decide whether the code should act on this even or not.
wf_cms.page_publish_now
wf_cms.page_publish_future
wf_cms.page_unpublish
wf_cms.background_page_publish
wf_cms.background_page_unpublish
Note that the background version doesn't make a distinction between Page
entities published now and ones published in the future. This is because for future publication, the job is scheduled to be ran at the (future) moment of publication - when that code is executed, the page will always be published "now".
Article events
Because doing additional work when articles are published is more likely than for other Page
types, Xalok also triggers these events only for articles:
wf_cms.article_publish_now
wf_cms.article_publish_future
wf_cms.article_unpublish
wf_cms.background_article_publish
wf_cms.background_article_unpublish