Add blocks to a container programmatically

Is there a way to add blocks to a container programmatically?
I assume its the same process as adding blocks to an area, by calling the addBlock method and passing the block type, arHandle and data.
I can add a block to an area, I can also add a container to an area but can’t figure out how to add blocks to a container.
From what I can see, for containers it uses the arHandle in the format of Main : 15 : Column 1 but I dont really understand where the “15” value is coming from. Guessing its something related to versioning or something but dont really see where its coming from.
I’m basically trying to import some contents and programatically add the content to my pages. I’ve done this before with just adding blocks to a page. But now I need to add containers to a page and then add contents.

Hello. THis is not tested but going through the core it seems to me this is how you’d do it.

You need to get a ContainerArea object first. You’ll need the container block instance (a container is a block) and the handle for the area.

$containerArea = new \Concrete\Core\Area\ContainerArea($containerBlockInstance, $areaDisplayName);

Then you get the SubArea from that $containerArea for the page you want to add blocks to

$subArea = $containerArea->getSubAreaObject($page);

Once you have your $subArea object, you use it normally in your addBlock() method.

I hope this helps.

1 Like