In this post, we will discuss how to create a block using custom module. The benefit of creating block from module is that we can theme the content of the block or we can use this block in any other site.
We can create the block using following step:
Step 1: Creating directory for the module
First we need to create a directory in the "sites/all/modules" directory of Drupal site. For example, we have created the directory "customblock". The name of the direcotry will become the name of your module. Now, we need to create two files in the "customblock" directory.
- customblock.info
- customblock.module
Step 2: Creating info file of the module
Now, we will create the info file of the module. This file will tell the Drupal about our module.
name = Custom block description = Creating block using custom module core = 7.x Package = Custom
Step 3: Creating module file
The block API has been changed since Drupal 6. In Drupal 6, there is only one hook used for all block operations but now in Drupal 7 there are different functions for different operations of the blocks.
hook_block_info() : This is used to tell Drupal about the new block or blocks that we will create. Drupal will display the block in the block list in the admin.
An implementation of hook_block_info() takes no arguments and is expected to return an associative array.
/** * Implements hook_block_info(). */
function customblock_block_info() {
$blocks = array();
//block array
$blocks['custom_bk'] = array('info' => t('Custom block'), 'cache' => DRUPAL_NO_CACHE, );
return $blocks;
}
Explanation
This defines a block named “custom_bk” that has two properties:
- Info: This provides a description about the block. The text is used on block list in admin.
- Cache: This tells Drupal how to cache the data from this block. In above example, I mentioned DRUPAL_NO_CACHE which tells Drupal not to cache the block.
There is a handbook to read about these in Drupal API http://api.drupal.org/api/function/hook_block_info/7
hook_block_view(): This is used to tell Drupal what to do with block when a block is requested for viewing. This is responsible for building the content of the blocks. This will call whenever Drupal display the block.
/*** Implements hook_block_view().*/
function customblock_block_view($block_name = '') {
if ($block_name == 'custom_bk’) {
$block = array( 'subject' => t('Custom module'), 'content' => ‘My first module’), );
return $block;
}
}
Explanation
The hook_block_view() takes the name of the block as arguments. If you will see the Drupal documentation, this arguments is mentioned as $delta.
We will check that if $block_name is the name of the block which we declared in the function “custom_block_info()” function, we need to return the content for the block.
The “custom_block_view()” function return the block array with two items in it
Subject : The title of the block
Content: The content of the block.
In our example, we have only display the static content in the block. We can also display the dynamic content in the block.
Comments
It has been simply strangely
It has been simply strangely open-handed with people like you to convey unreservedly what exactly a lot of people would have sold for an e book to make some bucks for their own end, primarily now that you might have done it in case you wanted.
http://www.directorise.com/listing/187240-Expert-Tips-on-Creating-Beauti... and http://www.dailystrength.org/people/1917227/journal
It has been simply strangely
It has been simply strangely open-handed with people like you to convey unreservedly what exactly a lot of people would have sold for an e book to make some bucks for their own end, primarily now that you might have done it in case you wanted.
http://www.directorise.com/listing/187240-Expert-Tips-on-Creating-Beauti... and http://www.dailystrength.org/people/1917227/journal
Add new comment