Automatically add editable block at page creation

Permalink
So... I already have a bunch of pages that have what I need where I need it. So if I add a block to the page type defaults, I'll have duplication in there.

What I would like to do is add some logic to my template that will alias a block from my global scrapbook if the alias doesn't already exist, and then have that block be editable (deletable) if I want.

I tried the following:
$b = Block::getByName('Where to Buy');
$page = Page::getCurrentPage();
$pages = $b->getCollectionList();                        
if( is_object($b) && !in_array($page,$pages)) {
   $b->duplicate($page);
}


I also tried $b->alias($page). While the if statement seems to be determining whether or not the alias exists, both the duplicate and alias methods are throwing errors.

Is there a way to do this? What am I doing wrong?

Thanks.

BigBobbyD
 
olsgreen replied on at Permalink Reply
olsgreen
Looks fine to me, whats the error that $b->alias() is throwing?
agedman replied on at Permalink Reply
agedman
It's an interesting question. It looks like part of the problem is that the duplicate() and alias() methods are expecting to see a block that has an arHandle (i.e. a regular block, not a global one).

So what if we tried just adding a regular block to a specific area of one specific page, then using your approach to alias it to the current page if it's not already there. I added a block to the 'Sidebar' area of my home page (cID=1) and noted the block's ID (in my case it was 241)

This sort of works:
Loader::model('Block');
$cc = Collection::getByID(1);
$b = Block::getByID(241,$cc,'Sidebar');
$page = Page::getCurrentPage();
if( is_object($b) {
  $pages = $b->getCollectionList();  
    if (in_array($page,$pages)) {
      $b->alias($page);
  }
}
One quirk is that the first time you go to a new page, the block doesn't show, but any time after that it does. You can't use duplicate() with this approach because each new block will get a new ID, so it will be added again and again.

A bigger problem is that after you've added the block to a bunch of pages this way, you run into trouble if you try to edit the block. The edited block gets a new bID so getByID() can't find it on the original page any more...
BigBobbyD replied on at Permalink Reply
BigBobbyD
Thanks for that... This won't work for my client, however, as it's pretty messy!

It looks like the arHandle thing is the culprit, as I went back and checked the error my code was throwing and there it is:

mysql error: [1048: Column 'arHandle' cannot be null] in EXECUTE("insert into CollectionVersionBlocks (cID, cvID, bID, arHandle, cbDisplayOrder, isOriginal, cbOverrideAreaPermissions) values ('151', '2', '380', NULL, 0, 1, 0)")

There must me a way to do this, though... With this information, I'm thinking I need to figure out how a global block is aliased, so checking out the way that a global block is aliased through the front end, I find this code in the html generated by elements/scrapbooklists.php (I think):

<a class="ccm-block-type-inner" style="background-image: url(/~robodev/concrete/blocks/html/icon.png)" href="javascript:void(0)" onclick="jQuery.fn.dialog.showLoader();$.get('/~robodev/index.php?pcID[]=27&amp;add=1&amp;processBlock=1&amp;cID=1&amp;arHandle=Where to Buy Heading&amp;btask=alias_existing_block&amp;&amp;ccm_token=1280787796:4a6495606e8b96dd166053d114a561ee', function(r) { ccm_parseBlockResponse(r, false, 'add'); })">HTML</a>


arHandle and the block ID are being invoked, as well as alias_existing_block. But I'm afraid my programming skills are pretty limited, and I'm sorta stuck here... If alias_existing_block is a method, I can't locate it in the documentation...