creating a new version programmatically

Permalink 3 users found helpful
hej!

how can i create a new version of a page by updating its blocks or itself (from within source code)?

i have a single page form which creates or updates a page. (all users from a certain group have one such a page, kinda similar to a profile page) when i now update the blocks within the page or the page itself (name, description) with their update method, no new version gets created.
as i want to approve the changes and be able to rollback to older versions, how can i update the page so they get created?

 
ijessup replied on at Permalink Best Answer Reply
ijessup
If its possible, I'd like to see the code to know what exactly you are doing, but it sounds like you want to do something along the lines of...
$currentCollectionVersion = Collection::getByID($cID)->getVersionObject();
$newCollectionVersion = $currentCollectionVersion->createNew($versionComments);
keinkoenig replied on at Permalink Reply
thats basically exactly what i was looking for, so biiiig thx! but how do i handle that thing correctly?
how do i get the page object of my new version?
does the pageID change due to the new version?
is the new version a blank page? if so, can i use the blocks from the other version and then alter them (or do i have to copy all the blocks myself)?
when i create a new page, can i set it to "to be approved", so it only shows up in the backends sitemap?

heres my code:
$page = Page::getByID($pD);
// update or create my block
$block = $this->getMyBlock($page);
if($block)
    $block->update($_POST);
else {
    $bt = BlockType::getByHandle('my_block');
    $page->addBlock($bt, 'Main', $_POST);   // names in forms are same as in database
}
//.. done with some more blocks
ijessup replied on at Permalink Reply
ijessup
You may, or may not, need this... but first a quick lesson

A Page is an extended Collection. That being said, what you called the pageID is synonymous with collectionID (referred to in the DB as a 'cID'). cIDs will never change, nor will cvIDs (collectionVersionIDs). If they did, the ID itself would be useless. However, what does change is which cvID is approved.

You don't have to 'get' the new version because you already have it. The variable '$newCollectionVersion' in my code example IS the new CollectionVersion object.

I'm fairly certain (could be wrong though) that the new version will not have any blocks on it. I don't see anything in the code that suggests blocks will be associated with a newly created CollectionVersion when using the createNew() method.

However, you can programatically alias old blocks (ones that don't need any modifications) and add new blocks to the newly created CollectionVersion.

The newly created CollectionVersion should not get automatically Approved. So you should see it in the list of pages 'Pending Approval'.

If you did want it automatically approved the code would look like this:
$newCollectionVersion->approve();
Simple enough, right!?

Hope that helps! :D
- Isaac
keinkoenig replied on at Permalink Reply
thx! you helped me a lot!
Guido replied on at Permalink Reply
If you did want the new version to have the same blocks, area styles and layout joins as the latest aproved version you could use the following code:

$_c= Page::getByID($cID);
$_c= $_c->cloneVersion(t('Version comments', $_c->getVersionID()));