Last active
December 12, 2017 06:51
-
-
Save rossriley/3d64b5ee4c49be21f92ba311bee5cea2 to your computer and use it in GitHub Desktop.
Modifying A Bolt Record on the Post Save Event
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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