Bolt CMS Events

I am working with the save event but having limited luck.

I have currently tried two ways but to limited success.

1) I can either never get the function to fire,

2) I am not too sure what to pass into the function for method two.

All I am trying to do is to dump the event information out on content save.Any help greatly appreciated, really loving this CMS

Attempt One -- never runs the function at all

class Extension extends BaseExtension
{


public function initialize() {
    $this->addCss('assets/extension.css');
    $this->addJavascript('assets/start.js', true);

    $this->app['dispatcher']->addListener(\Bolt\Events\StorageEvents::POST_SAVE, 'postSave');

}

function postSave(\Bolt\StorageEvent $event)
{
    dump($event);
}

Attempt two -- what do I input as a parameter?

class Extension extends BaseExtension
{


public function initialize() {
    $this->addCss('assets/extension.css');
    $this->addJavascript('assets/start.js', true);

    $this->app['dispatcher']->addListener(\Bolt\Events\StorageEvents::POST_SAVE,$this->postsave($this->?????));

}

function postSave(\Bolt\StorageEvent $event)
{
    dump($event);
}

Answers


The parameter needed is a php callback the format for this is something like this:

$this->app['dispatcher']->addListener(\Bolt\Events\StorageEvents::POST_SAVE, array($this, 'postSave'));

That syntax is saying to run the postSave method within the current class. So this would work with your example number 1.

Now you can dump the event in your postSave method and see the results.


Hopefully my answer doesn't come too late! You simply can modify the content and save it back to the database:

public function postSave(\Bolt\Events\StorageEvent $event) {
    // get the content
    $content = $event->getContent();
    // get a field out of the contenttype
    $data = $content->get("myField");
    // now modify $data here
    $data = "new data - what ever you want";
    // set data to the content
    $content->setValue("data", $data);
    // write the modified content to the database
    $this->app['storage']->saveContent($content);
}

Note that the function gets fired every time you save contents. So just add an if-statement like this to just modify content you really want to:

if ($event->getContentType() == "my_type")

Need Your Help

Cant get Values from JSON Webservice

asp.net ios xcode json web-services

I have a Webservice, which picks Data from a Database ans send them in JSON to the Iphone

Using ajax with a while statement and DB

php ajax

I have a question about using ajax.I want to delete something of my screen (to be clear, some articles) I'm generating content (the articles) on the screen with this code: