Archive for December 2013

How To Create A Custom Block In Drupal 7

Create a block from a custom module :  We use Drupal hooks to create a block from custom module. In Drupal 6, hook_block() was used. This function has been redefined in Drupal 7 as the following 8 functions: hook_block_configure – Define a configuration form for a block. hook_block_info – Define all blocks provided by the module. hook_block_info_alter – Change block definition before saving to the database. hook_block_list_alter – Act on blocks prior to rendering. hook_block_save – Save the configuration options from hook_block_configure(). hook_block_view – Return a rendered or renderable view of a block. hook_block_view_alter – Perform alterations to the content of a block. hook_block_view_MODULE_DELTA_alter – Perform alterations to a specific block.   Here we will develop a basic block and hence will use only hook_block_info() and hook_block_view().

Create a block using Drupal GUI :

  • Login to the website.
  • Navigate to Structure > Blocks > Add Block
  • Fill the empty fields and the necessary information:

Block Title: This is the title shown to the user. If you do not want to show any Title, write <none> Block Description: This is a brief description of the Block visible on block listing page. Block Body: Write the content of the block here. Region Settings: Blocks are placed into regions. Place your block on a region such as Header, Content, Footer etc. Note: You can navigate to Structure > Blocks and click on “Demonstrate block regions” to view the available regions as per your theme. The regions are theme specific. You can place the block in any of the enabled theme. Visibility Settings: You can tell Drupal on which page the block will be visible. The visibility settings can be set for Pages, Content Types, Roles and Users.