
attractive, usable web applications
Richard's Blog - Design, coding and life in Japan
Creating multiple blocks easily in a Drupal module pt2
Submitted by Richard on Tue, 06/08/2010 - 14:12Actually my first post on this subject didn't have too much to do with creating multiple blocks like in the multi-blocks plugin.
I took a look at the plugin but didn't like it because of the extra configuration and db calls seemed unnecessary for purely coded blocks anyway.
Here is how to create multiple instances of blocks that you may need for either pagination, navigation, links etc. Or for the case that you need to access the same block on different regions on your page.
Register Your Block
I will actually create 3 instances of this block, I want to use it on different areas depending on the page.
function MYMODULE_block($op = 'list', $delta = 0, $edit = array()) {
$block = array();
if ($op == 'list') {
$block['pagination']['info'] = t('Pagination Links');
$block['pagination']['cache'] = BLOCK_NO_CACHE;
$block['pagination']['region'] = 'content_top';
$block['pagination']['status'] = TRUE;
$block['pagination']['visibility'] = TRUE;
$block['pagination']['weight'] = '-7';
$block['pagination']['pages'] = 'result*';
$block['pagination-0']=$block['pagination'];
$block['pagination-0']['region'] = 'content_bottom';
$block['pagination-1']=$block['pagination-0'];
}
// View pulls a theme to pass back as block content
else if ($op == 'view') {
$themes = MYMODULE_theme();
if(isset($themes['block_'.$delta])) {
$block = array('content'=>theme('block_'.$delta));
}
}
return $block;
}
} NoticeI have the pagination-0 and pagination-1 keys in the block array. These give me my extra instances. But we need to add themes to handle the parsing of these.
Add to HOOK_theme to parse the block
I will create a theme registry but also add a helper method to help me extract the block prefixed items as well as those that have a '-' to separate instances of the block.
function MYMODULE_theme(){
define('MODULE_BLOCK', drupal_get_path('module', 'MYMODULE').'/themes/block');
$themes = array(
'block_pagination'=>array(),
'block_pagination-0'=>array(),
'block_pagination-1'=>array(),
);
return process_themes($themes);
}
function process_themes($themes){
$remove=array('block_');
foreach($themes as $key=>&$theme){
if(!isset($theme['path'])){
// set generic paths for list, block and detail pages
if(substr($key,0,6)=='block_') {
$theme['path']=MODULE_BLOCK;
}
}
if(isset($theme['path'])&&!isset($theme['template'])){
$theme['template']=str_replace($remove, '', $key);
}
if(!isset($theme['arguments'])){
$theme['arguments']=array();
}
// Allow for multiple blocks
if(strstr($key, '-')){
$parts = explode('-', $key);
$theme = $themes[$parts[0]];
}
}
return $themes;
}For more info on the implementation of this process_themes function please see my earlier post
Create your template file
Placed in sites/all/default/MYMODULE/themes/block/pagination.tpl.php, I have mine being pre-fed JSON data via MYMODULE_preprocess_block_pagination();
<?php if($pagination->total_entries > 0): ?>
<?php print t('Total Results'); ?>: <?php print $pagination->total_entries; ?> |
<?php print t('Current Page'); ?>: <?php print $pagination->current_page; ?> |
<?php if($pagination->current_page!=1): ?>
<?php print result_link('p', $pagination->current_page-1, t('Previous Page')); ?> <?php endif; ?>
<?php for($i=0; $i<count($pages); $i++): ?>
<?php $page = $pages[$i]; ?>
<?php print result_link('p', $page, $page); ?> <?php if(isset($pages[$i+1]) && $pages[$i+1]!=$page+1): ?>
...
<?php endif; ?>
<?php endfor; ?>
<?php if($pagination->current_page!=$pagination->total_pages): ?>
<?php print result_link('p', $pagination->current_page+1, t('Next Page')); ?> <?php endif; ?>
<?php endif; ?>Now we have the 3 instances of our block with 1 placed in content_top and 2 in content_bottom.
Now in admin/build/block you should see 3 sets of Pagination Links which you can add block settings to individually. Don't forget to empty the cache...
Tags:
Recent Blog Posts
- Why you should use Rails 3 and not Django 5th Sep 10, 12:28
- Doctrine 2 and why you should use it! 29th Jul 10, 15:23
- How to localize your main blog listing page using Drupal 26th Jun 10, 11:46
- Creating Rails like template helpers in Django 24th Jun 10, 22:01
- Error: No module named messagesdjango.contrib.contenttypes 22nd Jun 10, 22:00
Post new comment