Block Versioning

Permalink
I'm building out a module that maps additional data to blocks found in specific stacks. Basically:
1. create a stack and add blocks to it
2. go to my new dashboard, where it'll show a list of the blocks in the stack
3. you can make changes to a bunch of new settings that I've created for these blocks which are saved in a custom table, associated with each block's id.

I uncovered an architectural problem with this solution though. I had assumed that a block's ID was static, and there would just be an incremental block version when a block is updated. Apparently that isn't the case, instead if you edit and save a block a NEW block is created and published.

This means that I can't save my settings against a specific bID in the database because the bID changes as the block is updated.

Is there any way to map/track/find block IDs that are part of the same "version chain"? Given one bID can I get a previous version's bID? I just need to be able to link blocks that represent essentially the same block together.

I think I can probably accomplish this with more custom functionality and tables, but I'd love to use built in tools if this is possible.

Thanks for any advice!

 
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Starting from C5 8.4.0 you can use the event on_block_duplicate that will be dispatched in your case. It contains info for both the new and old block.

Just to be clear the event is for when a block is copied in the clipboard and added tot he page or stack AND for when a block is edited and hence gets a new ID.
dmx21 replied on at Permalink Reply
Appreciate it! Sounds like I can make something work with that. I'll give it a shot.
dmx21 replied on at Permalink Reply
Just to close the loop here for anyone looking at this in the future. I was able to achieve what I was looking for by putting this in my package's controller.php

use Concrete\Core\Support\Facade\Events;


public function on_start(){
        Events::addListener('on_block_duplicate', function($event) {
            $old = $event->getOldBlock();
            $new = $event->getNewBlock();
            \Log::info(sprintf('Block changed from %s to %s', $old->getBlockID(), $new->getBlockID()));
        });
    }


Also note that this appears to fire when a block is edited, pasted from clipboard, AND when being deleted. So you'll need to account for all of those cases.