I was given the task to create pages of existing data. The challenge: the pages contain editables and these editables should be filled by pre-existing data.
All editables must be created prior to creating pages with the content.
A editable consists of one or multiple elements. each element must be created individually and later added to the areablock which than is saved as a page.
The areablock is created with a name and later on all content gets connected via the name. In this example it is named content
for the ease of use.
This code creates a page construct, sets editables and saves it to the database
<?php
$editablesOnPage = [];
$pageInformationYouWantToCreate = [
'page_title' => 'My first page',
'folder_path' => '/',
'seo_description' => 'Pages seo description',
'seo_title' => 'Seo Title, if needed',
];
$positionOnPageFor = [
'headline' => 1,
'wysiwyg' => 2,
];
$foldertoCreateThePageUnder = 1; // Root folder id
$areaBlockName = 'content';
$importPage = new \Pimcore\Model\Document\Page();
$importPage->setKey($pageInformationYouWantToCreate['page_title']);
$importPage->setPath($pageInformationYouWantToCreate['folder_path']);
$importPage->setType('page');
$importPage->setTitle($pageInformationYouWantToCreate['page_title']);
$importPage->setDescription($pageInformationYouWantToCreate['seo_description']);
$importPage->setMetaData([
'seoTitle' => $pageInformationYouWantToCreate['seo_title'],
]);
$importPage->setParentId($foldertoCreateThePageUnder);
$importPage->setController('Default');
$blockArea = new \Pimcore\Model\Document\Editable\Areablock();
$blockArea->setName($areaBlockName);
// Headline
if ($pageInformationYouWantToCreate['headline_title']) {
$headline = new \Pimcore\Model\Document\Editable\Input();
$headline->setName(sprintf('%s:%s.title', $areaBlockName, $positionOnPageFor['headline']));
$headline->setDataFromEditmode($pageInformationYouWantToCreate['headline_title']);
$editablesOnPage[] = $headline;
}
// Wysiwyg
if ($pageInformationYouWantToCreate['content_text']) {
$wysiwygComponent = new \Pimcore\Model\Document\Editable\Wysiwyg();
$wysiwygComponent->setName(sprintf('%s:%s.text', $areaBlockName, $positionOnPageFor['wysiwyg']));
$wysiwygComponent->setDataFromEditmode($pageInformationYouWantToCreate['content_text']);
$editablesOnPage[] = $wysiwygComponent;
}
$blockArea->setDataFromEditmode([
[
'key' => $pageInformationYouWantToCreate['headline'],
'type' => 'heading',
'hidden' => false,
],
[
'key' => $pageInformationYouWantToCreate['wysiwyg'],
'type' => 'wysiwyg',
'hidden' => false,
]
]);
array_unshift($editablesOnPage, $blockArea);
$importPage->setEditables($editablesOnPage);
$importPage->save();