Populate default data in Sulu CMS
In our current Sulu CMS project a requirement was to populate default data when a content editor did not provide all needed information, e.g. social sharing information or SEO-related configurations.
Since Sulu CMS is built on top of the Symfony framework, this task can
be accomplished by developing an observer which is listening for a specific event. In this particular case, we are
interested in listening for the sulu_document_manager.persist
event which is emitted by Sulu's DocumentManager component.
Since Sulu CMS is using Symfony's EventDispatcher component,
our custom class needs to implement the Symfony\Component\EventDispatcher\EventSubscriberInterface
interface like this:
namespace App\Event;
use Sulu\Bundle\PageBundle\Document\BasePageDocument;
use Sulu\Component\DocumentManager\Event\PersistEvent;
use Sulu\Component\DocumentManager\Events;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PopulateDefaultExtensionData implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
Events::PERSIST => [
['handlePersist', 500],
],
];
}
public function handlePersist(PersistEvent $event): void
{
/** @var BasePageDocument $document */
$document = $event->getDocument();
if (!($document instanceof BasePageDocument)) {
return;
}
$extData = (array) $document->getExtensionsData();
if (!isset($extData['opengraph'])) {
$extData['opengraph'] = [
'og_title' => '',
'og_type' => '',
'og_description' => '',
'og_image' => null,
];
}
$documentTitle = 'The CMS | ' . $document->getTitle();
if (isset($extData['opengraph']['og_title']) && empty($extData['opengraph']['og_title'])) {
$extData['opengraph']['og_title'] = $documentTitle;
}
if (isset($extData['opengraph']['og_type']) && empty($extData['opengraph']['og_type'])) {
$extData['opengraph']['og_type'] = 'article';
}
if (isset($extData['seo'], $extData['seo']['title']) && empty($extData['seo']['title'])) {
$extData['seo']['title'] = $documentTitle;
}
$document->setExtensionsData($extData);
}
}
In the getSubscribedEvents()
method, we instruct the EventDispatcher to call the handlePersist()
method for
sulu_document_manager.persist
event.
Once the event is fired, we extract the BasePageDocument
and get its extension data. We check the existence of certain
fields in the extension data array and if not found, set our default values.
To register our implementation with the event dispatcher, the following configuration needs to be added to the services.yaml
file of your project:
app.event.populatedefaultextensiondata:
class: App\Event\PopulateDefaultExtensionData
tags: ['sulu_document_manager.event_subscriber']
The tag sulu_document_manager.event_subscriber
makes sure that Sulu's Document Manager will register our event subscriber
and dispatched events will reach our implementation.
Now, whenever a document is persisted via Sulu's DocumentManager, the event is dispatched and passed to our custom implementation. The extension data fields are checked and populated with the default data if needed.