Skip to content

Instantly share code, notes, and snippets.

@rossriley
Last active December 12, 2017 06:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rossriley/3d64b5ee4c49be21f92ba311bee5cea2 to your computer and use it in GitHub Desktop.
Save rossriley/3d64b5ee4c49be21f92ba311bee5cea2 to your computer and use it in GitHub Desktop.
Modifying A Bolt Record on the Post Save Event
<?php
namespace Bundle\Site;
use Bolt\Storage\Entity\Content;
use Bolt\Extension\SimpleExtension;
use Bolt\Events\StorageEvent;
use Bolt\Events\StorageEvents;
/**
* Site bundle extension loader.
*
* This is the base bundle you can use to further customise Bolt for your
* specific site.
*
* It is perfectly safe to remove this bundle, just remember to remove the
* entry from your .bolt.yml or .bolt.php file.
*
* For more information on building bundles see https://docs.bolt.cm/extensions
*/
class CustomisationExtension extends SimpleExtension
{
public static function getSubscribedEvents()
{
return [
StorageEvents::PRE_SAVE => [
['onPreSave', 0],
],
];
}
/**
* StorageEvents::PRE_SAVE event callback.
*
* @param StorageEvent $event
*/
public function onPreSave(StorageEvent $event)
{
$contentRecord = $event->getSubject();
$contentTypeName = (string) $contentRecord->getContentType();
if (!$contentRecord instanceof Content || !$contentTypeName === 'events') {
return;
}
$eventDate = strtotime($contentRecord->getEventdate());
$depublishTime = strtotime('+2 days', $eventDate);
$contentRecord->setDatedepublish($depublishTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment