Create page of PageType and customize blocks

Permalink 1 user found helpful
Hi,

I have a PageType which contains 2 blocks.
I want to create a page of that PageType and customize these 2 blocks but i don't want that these changes take effect on PageType
this is my code :
$parent = Page::getByID($this->post('cParentID'));
$ct = CollectionType::getByHandle('my_pagetype');
$data = array('cName' => $this->post('ctTitle'), 'cDescription' => $this->post('ctDescription'));
$p = $parent->add($ct, $data);
// Edit existing Page List Block
$eb = $p->getBlocks('Main');
if (is_object($eb[0])) {
    $block = $eb[0]->getInstance();
    $data = array();
    $data['cParentID'] = $p->getCollectionID();
    $data['num'] = $block->num;
    $data['cThis'] = $block->cThis;
    $data['ctID'] = $p->getCollectionTypeID();
    $block->save($data);
}

Is There a solution to create a copy of the block?
Thanks,

 
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Duplicate the block before changing its data. In your code, instead of "$block->save($data)", do this:
$new_block = $block->duplicate($p);
$new_block->getInstance()->save($data);
$block->delete(); //Delete the block that came from "page defaults" so you don't have 2 on the page
Darragi replied on at Permalink Reply
Thank you,it works!!