7

I'm using hook_block to create a block with the name of the custom module I'm creating.
I'm not being able to create a block without using myModuleName_block.

Do I need to do different modules for every different block I want to create?

1 Answer 1

15

You can make several blocks with hook_block, just use the $delta.

function hook_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {

    case 'list':
      $blocks[0]['info'] = t('Block 1');
      $blocks[1]['info'] = t('Block 2');
      return $blocks;

    case 'configure':
      if ($delta == 0) {
        // Block 1
      }
      else if ($delta == 1) {
        // Block 1
      }

 ....

}
Sign up to request clarification or add additional context in comments.

5 Comments

+1 - just one remark: The $delta doesn't need to be numeric, so if you want to provide a larger number of blocks, it is helpful for maintenance to use a meaningful 'name' instead.
just a small doubt... if i create multiple blocks like this, how can i invoke a particular block of a module in my page..
Does this solution require the blocks share the same template?
@AndrewShooner No, there are some rules for block templates, that makes it possible to share the block template among all blocks that a module has created, but this is a feature, not a requirement.
Just to clarify, I think the comment // Block 1 under $delta == 1 should be "Block 2" instead of "Block 1."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.