add block to page programmatically

Submitted by remo - 2 years ago

Sometimes you want to add a block programmatically when you create a page. This recipe shows you how to add a content block the the home page.

$homePage = Page::getByID(HOME_CID);
$contentBlockType = BlockType::getByHandle('content');

// block data, specific for each block, check db.xml or controller of block
// get more information about the necessary parameters.
$blockData = array('content' => 'My content');

// add block to Main area
$homePage->addBlock($contentBlockType, 'Main', $blockData);
Stats Categories Tags
comments powered by Disqus