Blog/ How To Create A Block In Drupal 7?

There are two ways to create a block in Drupal:

  • Using a custom module
  • Using Drupal GUI

Create a block using 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:

  1. hook_block_configure: Define a configuration form for a block.
  2. hook_block_info: Define all blocks provided by the module.
  3. hook_block_info_alter: Change block definition before saving to the database.
  4. hook_block_list_alter: Act on blocks prior to rendering.
  5. hook_block_save: Save the configuration options from hook_block_configure().
  6. hook_block_view: Return a rendered or renderable view of a block.
  7. hook_block_view_alter: Perform alterations to the content of a block.
  8. hook_block_view_MODULE_DELTA_alter: Perform alterations to a specific block.

Here we will develop a basic block (assuming that you already know how to create a custom module in Drupal 7) and hence will use only "hook_block_info()" and "hook_block_view()".

 * Implements hook_block_info().
function mymodule_block_info() {
  $blocks['mymodule-blockname'] = array(
    'info' => t('Administrative block title'),
    'cache' => DRUPAL_NO_CACHE,
  return $blocks;

 * Implements hook_block_view().
function mymodule_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'mymodule-blockname':
      $block['subject'] = t('Block Title');
      $block['content'] = t('Hello World!');
  return $block;

Create a block using Drupal GUI

  1. Log in to the website.
  2. Navigate to "Structure" -> "Blocks" -> "Add Block"
  3. 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.

Per Page Block Visibility Settings

All pages except those listed/Only the listed pages

In both cases, you can specify the pages by their paths (as shown above). The * is a wildcard character. For eg: as per the above setting, the block will be visible for the path and For frontpage, <front> can be used.

Pages on which this PHP code returns TRUE

Here you can write PHP code between <?php ?>. 

if ($condition) {
  // Condition is true. return TRUE to show the block.
  return TRUE;
else {
  // Condition is false. Return FALSE to hide the block.
  return FALSE;

Note:  "Pages Visibility Settings" and "Content Types Visibility Settings" do not work together.

Ready to get started?REQUEST A QUOTE