Programmatically create blocks using hook_block

Drupal API provide a hook function hook_block that allow developers to create block or set of blocks their way. hook is a keyword that will replace by the module name, for example example.module file will implement this hook as example_block()

For further details about this hook please visit here

Developers can create dynamic blocks and they can also create static blocks but believe me donot create static blocks using this hook.. drupal provides a clean way of creating static blocks on blocks admin screen.

Following is an example of a module which will define a block that will display visitors' IP and Origin details. The hook for creating blocks is appropriately called "hook_block". To implement this hook, in your example.module file create a hook function example_block().

The block function takes three parameters:

  • $op (or operation): which are "list", "view", "save", and "configure". This parameter tells your function which operation is called for. We'll discuss the "list" and "view" operation. The "configure" and "save" operations allow your block to have a settings form and save the settings
  • $delta: Your module can define more than one block in its "list" operation. Each block has a defined "delta" code, which is normally a number, and for the other operations, Drupal will pass in the "delta" value so you can identify which of a module's blocks to perform the operation on. This example module will only have two block.
  • $edit: used only with the "save" operation

The first operation is the "list" operation, which lists the blocks the module supplies, and defines how they will be seen on the Administer >> Site Building >> Blocks page

function example_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('IP Detection Block'),        
      );
      $blocks[1] = array(
        'info' => t('Country Detection Block'),        
      );
      return $blocks;

    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = "Your IP Address";
          $block['content'] = _example_ip_detection();
          break;
        case 1:
          $block['subject'] = "Country Detection";
          $block['content'] = _example_origin_detection();
          break;
      }    
      return $block;
    }
}

Create a function to be used internaly by this module that will return visitors' IP address.

function _example_ip_detection() {

 if (isset($_SERVER['HTTP_CLIENT_IP'])) {
  $ip = $_SERVER['HTTP_CLIENT_IP'];
  }
  elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  }
  elseif ( isset($_SERVER['REMOTE_ADDR']) ) {
    $ip = $_SERVER['REMOTE_ADDR'];
  }
  return $ip;
}

Create another function for internal use by this module that will return visitors' origin detail

function _example_origin_detection() {

 // Use your logic here
}

 

 

Comments

Mmm, awessome

Mmm, awessome