Monday, April 8, 2013

Create a block using custom module in drupal 7

In this post, we will discuss about creating block using custom module in drupal 7. The main benefit of build a block using custom module is connectable module. This code will act as separate connectable component.

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. This are the hook method provided by drupal 7 for build a block.

hook_block_info()
hook_block_configure()
hook_block_save()
hook_block_view()
hook_block_list_alter()
hook_block_view_alter()
hook_block_info_alter()
hook_block_view_MODULE_DELTA_alter()

Here we will discuss through different steps.

Step : 1
We need to create a directory in this path "sites/all/modules". i.e you need to create a new module folder inside drupal directory. Directory name must under-stable and meaningful related to your module purpose. I will named as "customblockmodule" for module directory.

We should have this two files inside directory.
1. customblockmodule.info
2. customblockmodule.module

Step : 2
Now we have to write a content for customblockmodule.info file.
Here we have mentioned name,description,core and package. Core  will have the drupal version for this module and package will define the area of module to be placed in module section.

Step : 3
In this module file, First we have to mention about info method

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.
This defines a block named “custom_bk_module” 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.


hook_block_view() : Will demonstrate about view of the block. It will hold the content of the block. When page requesting to view a block, this method will get called.

No comments:

Post a Comment