What parameters to pass when adding block programmatically?
Permalink Browser Info Environment
Hi - this is looking good and I'm trying to add a block programmatically. It's fine except I can't see what parameters to pass to set the view and edit permissions. Can you please correct the following;
$block = BlockType::getByHandle('file_tree'); $data = array( 'title' => 'File explorer for '.$userName, //works fine 'readrights' => '6', //no 'editrights' => '1' //no ); $Page->addBlock($block, 'Block Area name', $data);
Type: | Pre-Sale |
---|---|
Status: | In Progress |
Thanks; I am getting the group ID from the Groups table, the gID column. so 1 is Guest, for example, and 2 is Registered Users. Then in the call I have these in a comma delimited list; I just wonder if I have the handles wrong? I am calling like;
I've also tried
but still no joy. I could live with it allowing guest access, if that's feasible?
Thanks
'title' => 'File explorer for '.$userName, 'readrights' => '2,3', 'editrights' => '3,9'
I've also tried
'read_rights' => '2,3', 'edit_rights' => '3,9'
but still no joy. I could live with it allowing guest access, if that's feasible?
Thanks
Took me some digging, but finally found the right keys: "read" and "write" respectively. That should work. :)
Many thanks. Apologies for the delay in replying.
These work great but I've had to make a slight change to the controller code in the file_tree block.
For anyone trying to do the same, at the moment it's imploding the array but with the block call from php it's already a string.
So the code change at lines 245 and on to the next comment in the packages file_tree blocks file_tree controller file was;
Then call with the following code whenever you want to add the File Tree block;
Thanks again for your help.
These work great but I've had to make a slight change to the controller code in the file_tree block.
For anyone trying to do the same, at the moment it's imploding the array but with the block call from php it's already a string.
So the code change at lines 245 and on to the next comment in the packages file_tree blocks file_tree controller file was;
// Transform the read/write permissions to a string for inserting // in the block table. if(!isset($args['read'])) $args['read'] = array(); if(!isset($args['write'])) $args['write'] = array(); if (is_array($args['read'])) { $args['readrights'] = implode(',', $args['read']); }else { $args['readrights'] =$args['read']; } if (is_array($args['write'])) { $args['editrights'] = implode(',', $args['write']); }else
Viewing 15 lines of 19 lines. View entire code block.
Then call with the following code whenever you want to add the File Tree block;
$block = BlockType::getByHandle('file_tree'); //set block data $data = array( 'title' => 'File explorer for someone',//set the title 'read' => '2', //set which group(s) can read the block contents - get the numbers from the Groups table in your database 'write' => '3,6,8,9' //set which group(s) can write the block contents - get the numbers from the Groups table in your database ); $newBlock=$newPage->addBlock($block, 'Main', $data); //add the block to the area labelled "Main" $newBlock->setCustomTemplate("my_template"); //use the template you've developed
Thanks again for your help.
Best regards
Andy