As far as block creation goes, I thought of a way to create usermade blocks in php code. It's sort of an alternative to hook_block. We could use this to generate the blocks with the module, and then users could go in and modify their contents.
Here is an example of the function:
/**
* @function create_usermade_block. Generates blocks where the user can edit the contents. Alternative to hook_block.
* $block['title']..............The title of the block as shown to the user.
* $block['body']...............The content of the block as shown to the user.
* $block['info']...............A brief description of your block. Used on the block overview page.
* $block['format'].............Input format. Should probably be PHP. (value = 2)
**/
function create_usermade_block () {
$run_check = $variable_get('gjg_block_check', 1 );
//we only want to run this code once to create the blocks. User can edit them later.
if($run_check) {
$block1 = array('title' => 'GJG BLOCK TITLE #1','body' => 'GJG BLOCK BODY #1','info' => 'GJG BLOCK INFO NAME #1', 'format' => '2');
$block2 = array('title' => 'GJG BLOCK TITLE #2','body' => 'GJG BLOCK BODY #2','info' => 'GJG BLOCK INFO NAME #2', 'format' => '2');
$block3 = array('title' => 'GJG BLOCK TITLE #3','body' => 'GJG BLOCK BODY #3','info' => 'GJG BLOCK INFO NAME #3', 'format' => '2');
$block_array = array($block1, $block2, $block3);
foreach($block_array as $block) {
block_box_save($block);
}
variable_set('gjg_block_check', 0);
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #9 | block_generation.inc | 11.2 KB | zirafa |
| #8 | gjg.block_generation.patch | 9.84 KB | zirafa |
Comments
Comment #1
webchickHm. Not sure about this one... I prefer to use the existing infrastructure (e.g. admin/blocks) if possible. Or am I misreading this?
About access control, I don't think there's anyway to hide a block in its entirety in 4.6 from certain roles without it being embedded in the module's hook_block itself. However, we could do something similar to what's listed here:
http://drupal.org/node/13266
and just add a message to the block that says something to the effect of "You must be logged in to view this."
Comment #2
zirafa commentedAh, well this uses exactly the same callback function (block_box_save) that is used when a user creates a block through 'admin/block', so it's already using existing infrastructure. The only difference is I sent in the array directly instead of through a $_POST. It would just save the admin user time in configuration. Also I think the 4.6 access workaround you pointed out would be an adequate solution, I just don't know how ubiquitous these blocks are (i.e. would they be annoying?)
Comment #3
zirafa commentedOk updated versions of the functions. The first is how you create the blocks and the second is how you retrieve them. I still don't know how we should handle a reset or anything. Currently the first function is setup in such a way that it will only run once. We could theoretically have a reset function which deletes all the GJG blocks and then clears the $run_check flag, then runs create_usermade_block again. Let me know what you guys think.
Comment #4
zirafa commentedAdded a drupal_set_message to create_usermade_block. Usage of these functions would be to put them both in gjg.module, and then to call create_usermade_block by hook_settings. The second function would be called by the theme.
Now all we need is the actual list of blocks to generate using this thing...
?>
Comment #5
zirafa commentedAre these the only blocks that need to be generated?
Comment #6
webchickYep, those are them, plus one more:
"Recommendations"
This is just a simple hand-done block, containing the following:
Comment #7
zirafa commentedok these are up and working, with a reset function. all that's needed now is to integrate into gjg.module and delete the hook_block style ones (but keep the user_block one and load the code from here).
'; //end of the code_block;
return $code_block; //return the above php snippet.
case 'group_actions':
$code_block = '
';
return $code_block; //return the above php snippet
case 'my_friends':
$code_block = '
'; //end php snippet
return $code_block; //return the above php snippet
case 'personal_content':
$code_block = '
'; //end of php snippet
return $code_block; //return above snippet
case 'user_block':
$code_block = '
'; //end of php snippet
return $code_block; //return above php snippet
case 'recommendations':
$code_block = '
'; //end of php snippet
return $code_block; //return the above code snippet
}
}
function gjg_reset_blocks() {
$run_check = variable_get('gjg_block_check', 0 );
if(!$run_check){
$blocks = array('GJG: Group profile','GJG: Group actions','GJG: My friends','GJG: Recommendations','GJG: User block *CODE ONLY*','GJG: Personal content');
//delete the current blocks
foreach($blocks as $block_info) {
$bid = variable_get($block_info, 0);
if($bid) {
$box = block_box_get($bid);
$info = $box['info'] ? $box['info'] : $box['title'];
db_query('DELETE FROM {boxes} WHERE bid = %d', $bid);
}
}
cache_clear_all();
//recreate the default blocks
variable_set('gjg_block_check', 1 );
gjg_create_usermade_blocks();
drupal_set_message(t('The previous GJG blocks have successfully been deleted and re-created.'));
}
}
?>
Comment #8
zirafa commentedPatch to replace GJG blocks with user_editable blocks.
gjg_settings() will install the blocks only once if not installed. the reset checkbox on the settings page will overwrite the previous blocks with new ones.
hook_block is still used to create 'GJG: User block' although it's contents can be edited in GJG: User block *CODE ONLY*.
It is up and running on the dev site. The only thing left is to modify block.tpl.php and coordinate with trae to get these blocks themed properly, which will probably happen tomorrow (today?) morning.
Farsheed
Comment #9
zirafa commentedThis file needs to be added to the path gjg/block_generation.inc. Don't know if the patch contained this.
Comment #10
zirafa commentedadditionally, group_block.module is no longer used.
Comment #11
webchickUpgrading this to 'critical', however I'm going to wait until very near the end to integrate this since the block spec is still in flux.