How to create block in custom module in Drupal?

1. First of all you need to use hook_block() to create block in custom module.
2. See the below code for creating block.

function apqc_benchmarking_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[1]['info'] = t('First Block');
    $blocks[1]['cache'] = BLOCK_NO_CACHE;
    $blocks[2]['info'] = t('Second Block');
    $blocks[2]['cache'] = BLOCK_NO_CACHE;

    return $blocks;
  elseif ($op == 'view') {
    switch ($delta) {
      case 1:
        $block['subject'] = t('First Block');
        $block['content'] = theme('apqc_benchmarking_first_time_here');
      case 2:
        $block['subject'] = t('Second Block');
        $block['content'] = call_any_function();
    return $block;

In above code, I have created two blocks named First Block and Second Block.
In $op='list', I am listing the block names. You can add multiple blocks here.
In $op='view', You can display whatever you want to show.
For example, you can call any theme function or any function which is returning any HTML content.

No comments:

Post a Comment