Page Default Blocks Not Added When Page Added Programatically

Permalink
I have a page type where the default template has been setup with a composer control. When a page is added through the dashboard, the default blocks are added as expected.

When the page is added programatically, this does not happen. The page's areas are empty
Here's my code
$type = PageType::getByHandle('full');
$page = Page::getByID(1);
$params = array(
   'cName' => 'My Page',
   'cDescription' => 'My Description,
);
$newPage = $page->add($type, $params);


Anybody know why this is happening?

jero
 
hutman replied on at Permalink Reply
hutman
This code looks good other than the missing ending quote on the cDescription, I have used this code and it works and includes the default blocks.

use Concrete\Core\Support\Facade\Application as Core;
use Concrete\Core\Page\Page;
use Concrete\Core\Page\Type\Type as PageType;
$txt = Core::make('helper/text');
$parentPage = Page::getByPath('/products/categories');
$pt = PageType::getByHandle('category');
$data = array(
    'cName' => h($name),
    'cHandle' => $txt->sanitizeFileSystem($name)
);
$newPage = $parentPage->add($pt, $data);


What version of c5 are you using?
jero replied on at Permalink Reply
jero
it's 8.4.5

The issue seems to be that the page default has a composer control block which is not getting the corresponding block it's linked to added to the page. A regular block e.g. content is added just fine, but I need the pages to work with Composer.

It makes me wonder if there's an entirely different process for adding and then publishing a page draft programmatically in the same manner that Composer would?
jero replied on at Permalink Reply
jero
It turns out the process for adding a page that must work with Composer is a little different. Once I'd realised it was the Composer Control that was not being added, it made me wonder, and then I found this post

https://www.concrete5.org/community/forums/customizing_c5/add-page-p...

and from that I came up with this:

$th = $this->app->make('helper/text');
/* @var $th \Concrete\Core\Utility\Service\Text */
$pagetype = PageType::getByHandle('our_work');
$template = $pagetype->getPageTypeDefaultPageTemplateObject();
$parent = Page::getByID(1);
$params = array(
      'cName' => 'My New Page',
      'cDescription' => 'New Description',
      'cHandle' => $th->urlify('My New Page')
);
$newPage = $pagetype->createDraft($template, $user = false);
/* @var $newPage Page */
$newPage->setPageDraftTargetParentPageID($parent->getCollectionID());
$newPage->update($params);
$saver = $pagetype->getPageTypeSaverObject();


which worked.