Drupal Module Development Demonstration of Blocks :
Hello Guys ! Drupal Module development is great way to enhance Drupal functionality ,So Today i gonna go for Demonstration of Block in Drupal Module Development .Block are very important in Drupal so I am gonna create a sample Block module which we can place in any block of our theme.Like I m gonna place this sample block content in sidebar of theme.We will be implementing block hook in the Drupal .So first thing before movin to tutorial is that you must read the below listed tutorials first.
Drupal Module Development Demonstration of Blocks:Tutorial -
So here we go we gonna start with our tutorial for Drupal Block .So we gonna create the Sample Block module from the knowledge gained through above two Tutorials.we gonna name our module trails module and the we gonna create a folder named “trails” in “sites/all/modules/custom” so that we end up like “sites/all/modules/custom/trails” .
So Lets See the Steps in Demonstration of Block Module.
Drupal Demonstration of Blocks Hook : Module info file(trails.info) -
The Our First Step is to Create info file for our module .I hope you know info file about from my other post.So lets see info file code.
name = Trails description = "Demonstration of trails Module" package = Build a Module core = 7.x php = 5.1
This is a Simple info file for trails module which tell about our module.
Demonstration of Blocks:Module File(trails.module & trails.admin.inc)-
Lets take a look at our main module files which are trails.module and trails.admin.inc .yes we have two files the trails.admin.inc file is used by the menu_hook implemented in the trails module .which makes a menu page in the drupal configuration page.So lets take a look at both of our files.i am gonna paste both file here then gonna describe each function individually after that.
Sites/all/modules/custom/trails/trails.module :
<?php
// $id:$
/**
* Implements hook_init()
* Executes on every page load
*/
function trails_init(){
//ddebug_backtrace();
//Grab the trail history from a variable
$trail = variable_get('trails_history',array());
//Add current page to trail.
$trail[] = array(
'title' => strip_tags(drupal_get_title()),
'path' => $_GET['q'],
'timestamp' => REQUEST_TIME,
);
//Save the trail as a variable
variable_set('trails_history',$trail);
}
/**
* Implements hook_permission()
*/
function trails_permission(){
//ddebug_backtrace();
return array(
'administer trails' => array(
'title' => t('Administer Trails Module'),
'description' => t('Perform administration tasks for Trails module.'),
),
'access trails blocks' => array(
'title' => t('Access Trails Blocks'),
'description' => t('View blocks generated by the Trails module.'),
),
);
}
/**
* Implements hook_menu()
*/
function trails_menu(){
//ddebug_backtrace();
$items['admin/config/trails'] = array(
'title' => 'Trails',
'description' => 'trails configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('trails_admin_settings'),
'access arguments' => array('administer trails'),
'file' => 'trails.admin.inc',
'file path' => drupal_get_path('module','trails'),
);
return $items;
}
/**
* Implements hook_cron()
*/
function trails_cron(){
//debug_backtrace();
//Get the trail array.
$trail = variable_get('trials_history', array());
//Get the offset of array_slice, so we can save just last 5 items.
$count_minus_5 = count($trail)-5;
//Cut out everything but the last 5 visits.
$short_trail = array_slice($trail,$count_minus_5);
//Save the shorter trail
variable_set('trails_history',$short_trail);
}
function trails_block_info(){
$blocks['history'] = array(
'info' => t('History'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
function trails_block_configure($delta = ''){
//Get the maximum allowed value from the configuratino form
$max_to_display = variable_get('trails_block_max',50);
//add a select box of numbers form 1 to $max_to_display.
$form['trails_block_num'] = array(
'#type' => 'select',
'#title' => t('Number of items to show'),
'#default_value' => variable_get('trails_block_num',5),
'#options' => drupal_map_assoc(range(1,$max_to_display)),
);
return $form;
}
function trails_block_save($delta = '', $edit=array()){
variable_set('trails_block_num',$edit['trails_block_num']);
}
function trails_block_view($delta = ''){
if(user_access('access trails blocks')){
//Create list of previous paths.
//grab the trail history from a variable
$trail = variable_get('trails_history',array());
//Flip the saved array to show newest pages first.
$reverse_trail = array_reverse($trail);
//Grab the number of items to display
$num_items = variable_get('trails_block_num','5');
//Output the latest items as a list
$output='';
for($i=0; $i<$num_items; $i++){
if($item = $reverse_trail[$i]){
$output .= '<li>'. l($item['title'],$item['path']).' - '.format_interval(REQUEST_TIME - $item['timestamp']).' '.t('ago').'</li>';
}
}
if(isset($output)){
$output = '<p>' . t('Below are the last !num pages you have visited.', array('!num' => $num_items)). '</p><ul>'.$output.'</ul>';
}
//prepare to return the $block variable with subject (title) and content (output).
$block['subject'] = 'History';
$block['content'] = $output;
return $block;
}
}
Now lets take a look at another file named trails.admin.inc which is used by main module file.
Sites/all/modules/custom/trails/trails.admin.inc :
<?php
//$id:$
function trails_admin_settings(){
$form['trails_block_max'] = array(
'#type' => 'select',
'#title' => t('Maximum number of items to display'),
'#options' => drupal_map_assoc(range(1,200)),
'#default_value' => variable_get('trails_block_max',''),
'#description' => t('This will set the maximum allowable number that can be displayed in a history block.'),
'#required' => TRUE,
);
return system_settings_form($form);
}
The Above are all the three files which we need for our module.The Module should be complete now and you can copy it but still we are not finished yet ,i m gonna explain each and very hook that we implemented on this module.
Drupal Demonstration of Blocks: Function description -
Now we are going to see the hooks implemented and each and every function which i have used in the module.in this blocks demonstration module the following hooks has been implemented.
- hook_init – trails_init()
- hook_permission – trails_permission()
- hook_menu – trails_menu()
- hook_cron – trails_cron()
- hook_block_info – trails_block_info()
- hook_block_configure – trails_block_configure()
- hook_block_save – trails_block_save()
- hook_block_view -trails_block_view()
/**
* Implements hook_init()
* Executes on every page load
*/
function trails_init(){
//Grab the trail history from a variable
$trail = variable_get('trails_history',array());
//Add current page to trail.
$trail[] = array(
'title' => strip_tags(drupal_get_title()),
'path' => $_GET['q'],
'timestamp' => REQUEST_TIME,
);
//Save the trail as a variable
variable_set('trails_history',$trail);
}
the function is self describing it get the trails_history variable from the dbase. if the variable is not set yet then initialize with array.The $trail array get the current page title and path and time and then store back in trail_history in to the dbase.
/**
* Implements hook_permission()
*/
function trails_permission(){
//ddebug_backtrace();
return array(
'administer trails' => array(
'title' => t('Administer Trails Module'),
'description' => t('Perform administration tasks for Trails module.'),
),
'access trails blocks' => array(
'title' => t('Access Trails Blocks'),
'description' => t('View blocks generated by the Trails module.'),
),
);
}
creates two permissions set which can be managed from admin panel.one permission is created for configuration and other for block view i.e who can view the blocks,the block we gonna create in next block hooks.
/**
* Implements hook_menu()
*/
function trails_menu(){
//ddebug_backtrace();
$items['admin/config/trails'] = array(
'title' => 'Trails',
'description' => 'trails configuration',
'page callback' => 'drupal_get_form',
'page arguments' => array('trails_admin_settings'),
'access arguments' => array('administer trails'),
'file' => 'trails.admin.inc',
'file path' => drupal_get_path('module','trails'),
);
return $items;
}
Trails_cron function: implements the hook_cron.it only runs when the cron of drupal is run.lets see the function.
/**
* Implements hook_cron()
*/
function trails_cron(){
//debug_backtrace();
//Get the trail array.
$trail = variable_get('trials_history', array());
//Get the offset of array_slice, so we can save just last 5 items.
$count_minus_5 = count($trail)-5;
//Cut out everything but the last 5 visits.
$short_trail = array_slice($trail,$count_minus_5);
//Save the shorter trail
variable_set('trails_history',$short_trail);
}
Now this function actually takes the trail_history variable from the dbase and shorten it to lenght 5 so that it stores only last five values i.e keys in case of array .then store it back to dbase.This is the only task of this fucntion.
Trails_block_info function: implements the hook_block.This is the important one as this post is about demonstration of block.the block_info hook create a new block with your desired name,detail .say you create module in drupal you provide info file ,similar is you create drupal block and you implement the block info hook ,which tells drupal about the block.
function trails_block_info(){
$blocks['history'] = array(
'info' => t('History'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
Trails_block_configure function: implements the hook_configure.now after providing the info about the module we gonna configure it .here this hook simply renders on the block configuration page. which would be at this address ‘admin/structure/block/manage/trails/history/configure ‘ .here you can configure your block to show number of pages to show .this hook creates a select box .from which we can select how many last visited item we wana show in out block.
function trails_block_configure($delta = ''){
//Get the maximum allowed value from the configuratino form
$max_to_display = variable_get('trails_block_max',50);
//add a select box of numbers form 1 to $max_to_display.
$form['trails_block_num'] = array(
'#type' => 'select',
'#title' => t('Number of items to show'),
'#default_value' => variable_get('trails_block_num',5),
'#options' => drupal_map_assoc(range(1,$max_to_display)),
);
return $form;
}
trails_block_save function : implements the hook_block_save .used by drupal to save block configguaration.
function trails_block_save($delta = '', $edit=array()){
variable_set('trails_block_num',$edit['trails_block_num']);
}
trails_block_view :implements the hook_block_view .this is the actual block which we see.this will render on or sidebar.
function trails_block_view($delta = ''){
if(user_access('access trails blocks')){
//Create list of previous paths.
//grab the trail history from a variable
$trail = variable_get('trails_history',array());
//Flip the saved array to show newest pages first.
$reverse_trail = array_reverse($trail);
//Grab the number of items to display
$num_items = variable_get('trails_block_num','5');
//Output the latest items as a list
$output='';
for($i=0; $i<$num_items; $i++){
if($item = $reverse_trail[$i]){
$output .= '<li>'. l($item['title'],$item['path']).' - '.format_interval(REQUEST_TIME - $item['timestamp']).' '.t('ago').'</li>';
}
}
if(isset($output)){
$output = '<p>' . t('Below are the last !num pages you have visited.', array('!num' => $num_items)). '</p><ul>'.$output.'</ul>';
}
//prepare to return the $block variable with subject (title) and content (output).
$block['subject'] = 'History';
$block['content'] = $output;
return $block;
}
}
Now we have learned all the function lets see hwo the module works .Firtly you can copy the all code and create three file or can download the sample from link below:
If you downloaded the module then extract it to /sites/all/modules/custom/trails .
Then Go to to modules in drupal admin and enable it.
After enabling it go to “admin/structure/block ” there you’ll find history block .Set it to show on your sidebar.
Then click Save Blocks.
After that visit your homepage .You can see the block history which stores last five visits.
That is all i hope you liked this post..thanks for visiting designaeon.com.

