Programatically editing a content block within a page

Permalink 2 users found helpful
Hi, All --

Given a page that I had just created from a page type, I was trying to find a (content) block and edit it with the "starter" text.

I spent a few hours trying to figure this out last night, and (surprisingly) couldn't find any help on the forums. Thought I'd document it here.

I was having tons of problems with the fact that the block is set up as an "alias" (to the "default" block held on the page type) and trying to de-alias the block. Then I realized I was running into caching issues, which made finding the solution harder (I assumed you get the block's controller instance in order to ->save(), but don't!).

Without further adieu:

$nb = $this->contentBlockObj->duplicate($this->pageObj); //pageObj is the same page the existing block is on... you can probably get it from the Block, but I had it stored for other reasons
$this->contentBlockObj->delete();
$nb->update(array('content' => $this->descr));
$this->contentBlockObj = $nb;


James

jshannon
 
seemeu replied on at Permalink Reply
James, I tried to implement your solution, but keep running into "the wall of unknown." :) Here's my code:

$blocks = $newPage->getBlocks('Sidebar');
foreach ($blocks as $b){
   $bID = $b->getBlockID();
   Log::addEntry('Group '.$group_name.' block id is '.$bID. ' and block handle is '.$b->getBlockTypeHandle().' and block type ID is '.$b->getBlockTypeID().' and block type name is '.$b->getBlockTypeName());
   $nb = $b->duplicate($newPage);
   $b->delete();
   $nb->update(array('frontend_access' => 'group', 'access_groupID' => $gID, 'fsID' => $fsID_bg ));
   $b = $nb;
   }


$bID in the log shows 944, which refers to the page I created for test purposes a while ago. The most recent id in the DB is 1080, and I expected to log a block ID that followed 1080, not preceded it.

Is the newly created page pointing to the page type default's block ID? If so, how do I get the current instance of the block on the newly created page and update it?

Lots of thanks in advance for any help! I'm stuck...
jshannon replied on at Permalink Reply
jshannon
Hi..

To be honest, I don't really remember. It was one of those solutions that wasn't at all logical to me and required a lot of trial and error, and thus the solution didn't "click".

I think getting a bID of 944 is correct. That's the whole point of doing the ->duplicate() -- even a brand new page starts with a reference to the old blog that's part of the template. There is no "current instance".

But then I see that you' do the $b->duplicate() as I had done, and $b->delete(). So, basically, everything looks correct. Now, if you logged "block id is " . $nb->getBlockID(), you should have the new one.

Basically, what exactly is wrong, other than getting a confusing block id? Is your page not updating? Is the database not getting a new block?

James