Breaking News
You are here : Home / Drupal / Drupal Module Development – Demonstration of Blocks
Drupal Module Development Block demonstration

Drupal Module Development – Demonstration of Blocks

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.

So you must read the above two tutorials before moving on this one.

[separator /]

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( –

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.

[separator /]

Demonstration of Blocks:Module File(trails.module &

Lets take a look at our main module files which are trails.module and .yes we have two files the 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 :

Now lets take a look at another file named which is used by main module file.

Sites/all/modules/custom/trails/ :

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.

[separator /]

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 this blocks demonstration module the following hooks has been implemented.

  1. hook_init –  trails_init()
  2. hook_permission – trails_permission()
  3. hook_menu – trails_menu()
  4. hook_cron – trails_cron()
  5. hook_block_info – trails_block_info()
  6. hook_block_configure – trails_block_configure()
  7. hook_block_save – trails_block_save()
  8. hook_block_view -trails_block_view()
These are the all hooks i have implemented on this module .Lets see each of the above hook implemented.
Trails_init  function: this hook executes on every page load .so lets see what it do.

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.

[separator /]

Trails_permission function: this implements the permission hook i m not gonna explain it as i assume you read my previous drupal tutorial which i suggested before this tutorial .but you can see the function below:

creates two permissions set which can be managed from admin 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.
[separator /]

Trails_menu function: implements the hook_menu ,creates a menu page for the module.In this case the configuration page is created.which is located at /admin/config/trails .when you visit this url you’ll see trails can see the the page callback is drupal_get_form and then the page argument is the take a look at .which is used to set the maximum possible value in variable trails_block_max.

Trails_cron function: implements the only runs when the cron of drupal is run.lets see the function.

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.

[separator /]

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.

WE have created new block with name history which is key in the array.from now your mind starting to ringing that this module keeps the history about the recently visited adresses in drupal admin panel.
Trails_block_configure function: implements the 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.

[separator /]
trails_block_save function : implements the hook_block_save .used by drupal to save block configguaration.

[separator /]
trails_block_view :implements the hook_block_view .this is the actual block which we see.this will render on or sidebar.

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:

[jbutton icon=”download” link=”” size=”xlarge”]Download Module[/jbutton]

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.

Demonstrating block in drupal module development

Drupal Module- Trails

After enabling it go to “admin/structure/block ” there you’ll find history block .Set it to show on your sidebar.

Drupal Blocks demonstration

Trails History Block

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
[separator /]

Tags : , , , , ,

Leave a Reply

Want to join the discussion ? Feel Free to contribute !