"Unable to load block into composer." when adding Page Programatically

When I’m adding Pages programatically with:

$pageTemplate = \PageTemplate::getByHandle(strtolower($treename));
$pageType = \PageType::getByHandle(strtolower($treename));
$page = $parentPage->add(
                                'cName' => $xlsxrow[$pageTitleId],
                                'cDescription' => '',
                                'cHandle' => $newPagePath
                        $page->update(['cName' => $xlsxrow[$pageTitleId]]);

I cannot Edit the Templates Composer Blocks.

Any Ideas what I’m doing wrong?

It’s a while since I’ve done this, but from some fairly ancient notes that I have, I believe you need to create a new page draft. This is what my notes say:

$pagetype = PageType::getByHandle('sidebar');
$template = $pagetype->getPageTypeDefaultPageTemplateObject();
$parent = Page::getByID(1);
$th = $app->make('helper/text');

$newPage = $pagetype->createDraft($template, $u = false);
 /* @var $newPage Page */
$newPage->setPageDraftTargetParentPageID( $parent->getCollectionID());
 $params = array(
         'cName' => $name,
          'cDescription' => $name,
           'cHandle' => $th->urlify($name)
$saver = $pagetype->getPageTypeSaverObject();
$publishDateTime = false;
$pagetype->publish($newPage, $publishDateTime);

Thereafter, you would do something like this to update the block content:

$newarea = new Area('Main');
$newblocks = $newarea->getAreaBlocksArray($newPage);
foreach  ($newblocks as $newBlock){
       if ($newBlock->getBlockTypeHandle() === 'my_block_handle') {
              $clr = $newBlock->getController();
//Substitute whatever parameters the block needs:
              $clr->save(['title'=>$name, 'overview'=>$name, 'content'=>$content);

Perfect, that works like a charm!

Thanks a lot!

You’re welcome. Glad it worked for you.

1 Like