Block->getBlockCollectionObject()

Permalink
After adding a new block, I tried fetching the collection path off the current block object, but with no success. All that what was sent back to the browser, was an collection object with the attribute error=10 and the version object.
In the block's save method, I called the parent's save before fetching the collection object. Still I didn't got to a valid page object, from which I could read its path.
Now, each block has its own variable "c" and "cID" which, whilst editing a block, is set correctly.
So comes my questions: When is the collection object set?
Or is there any workaround to get the path of a newly created block?

weltenschmid
 
andrew replied on at Permalink Reply
andrew
This is not documented and I'm not exactly sure where in the flow you're wanting to do this (it's in the block controller's save() method, right?)

If you're inside the block controller saving the block, try this

if (isset($this->pobj)) {
$c = $this->pobj->getBlockCollectionObject();
}

Does that return anything more useful, in terms of a page object?

(This is obviously one area of the API that needs some attention.)
Remo replied on at Permalink Reply
Remo
he's in the save method but the code he wrote already uses pobj and he hasn't changed anything during the last few days..

is it possible that pobj is only available when editing a block?

I thought he told me that, it doens't work when adding a new block, even after parent::save has been called.
andrew replied on at Permalink Reply
andrew
Sorry - that was already mentioned but I missed it. Must be too early in the morning for my head.

Yes, unfortunately the way we pass all this data around the block is created before it's assigned to a page, so the block object has no information about what page it's going to be added to at that point.

This is something that we should fix, just for the sake of the API. In the meantime, you could try adding this to your save method:

<?php
$currentPage = Page::getByID($_REQUEST['cID'], 'RECENT');
$path = $currentPage->getCollectionPath();
?>


Not as elegant, but it should work and the cID will always be present in these situations.
Remo replied on at Permalink Reply
Remo
I thought parent::save create this relation but your code is fine.

As long as it works..

Thanks!
andrew replied on at Permalink Reply
andrew
At that point, the system only knows the type of block you're adding. It adds the block to the system, and saves the block-specific data...but then it returns this new block object to the routine that is responsible for relating it to the correct page.