Convert a 'Block' into a 'Package'

Permalink 2 users found helpful
I have a client that currently has a custom block installed and is using it on their site.

They have some functionality that they want added to it and I would like to convert it into a package (instead of a stand-alone block). Is there a 'best' way to do this? Or at least an 'okay' way?

Is it simply a matter of updating the pkgID for the block in the BlockTypes table?

SkyBlueSofa
 
enlil replied on at Permalink Reply
enlil
I've pondered this as well. You would think if you cloned the block into a package and updated the package id and any new database stuff you needed it *should* work?
SkyBlueSofa replied on at Permalink Reply
SkyBlueSofa
It seems like adding the packageID to the BlockType record is a good step in the right direction. Here's what I did:

* Let's say the block was 'My Cool Block' and was located in the {root}/blocks/my_cool_block directory.
* The new package has as one if it's blocks 'My Cool Block'.

You can't just willy-nilly install My Cool Block like you would in a new package. Instead, I used this code in the package::install function:
$blockHandle = 'my_cool_block';
$myCoolBlock = BlockType::getByHandle($blockHandle);
if (!$myCoolBlock) {
   BlockType::installBlockTypeFromPackage($blockHandle, $pkg);
} else {
   $db = Loader::db();
   $sql = "UPDATE BlockTypes SET pkgID=? WHERE btHandle=?";
   $db->execute($sql, array($pkg->getPackageID(), $blockHandle));
   $myCoolBlock->refresh();
}


If the block doesn't exist, it will be installed. If it does exist, it will be associated with this package and the database table will be updated.

After that, you'll need to remove the existing block code from the {root}/blocks directory.
goldhat replied on at Permalink Reply
Very savvy solution.