Determine if a Block was Added or Edited

Permalink
Hi all, I have a question for you C5 gurus.

I'm building a package that includes a few block types. In one of the block types, I need to execute some code ONLY WHEN THE BLOCK IS FIRST ADDED, but not when an existing block is edited and saved.

I'm not talking about running the code in the add.php & edit.php forms - I want to run the code when the block is saved.

It seems natural that my code should go in the block controller's save() function, but how can I determine if the save() call is the first time the block is being saved (an "add") or a subsequent save (an "edit")?

Is there a magic function like on_add() I can add in my controller? or what's the best way to approach this?

Thanks in advance for any advice.

tomreitz
 
tomreitz replied on at Permalink Reply
tomreitz
I think I figured it out... posting my solution here in case it's useful for anyone else.

It looks like
$this->record->_saved
when called in a block controller's save() function, is a boolean variable that will be true if the block has been previously saved (so it's being EDITED now), or false if the block has NOT been previously saved (so it's being ADDED now).



I'm surprised there's not a method in the BlockController class for accessing this property - say, $controller->isSaved(). Maybe it can be added in the future? This solution is a bit clunky.
jordanlev replied on at Permalink Reply
jordanlev
I think you can just check if the block ID ($this->bID) is empty -- it shouldn't be set to anything until parent::save() is called.