0

I've created a module which creates 2 blocks.

function my_module_block_info() {

  $blocks['blockA'] = array(
    'info' => t('blockA'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );

  $blocks['blockB'] = array(
    'info' => t('blockB'),
    'cache' => DRUPAL_CACHE_PER_ROLE,
  );
  return $blocks;
}

function my_module_block_view($delta = '') {
  switch ($delta) {
    case 'blockA':
      $block['subject'] = t('subjectA');
      if (user_access('access content')) {
          $block['content'] = 'HelloA';
      }
      return $block;
      break;

    case 'blockB':
      $block['subject'] = t('subjectB');
      if (user_access('access content')) {
          $block['content'] = 'HelloB';
      }
      return $block;
      break;
}

Everything good so far. Here is my question:

I would like developers to create blocks using the same module (20 more blocks). How can I add a new block with its view without modifying the my_module.module? Is there a way to extend and include .php files that declare other blocks? So, instead of having a huge my_module.module file, I can have multiples .php files (one for each block).

Thank you in advance

1 Answer 1

2

* Method 1 - all blocks in separate file *

You can implement hook_hook_info() in your .modules file like this:

function hook_hook_info()
{
  $hooks['block_info']['group'] = 'block';
  $hooks['block_view']['group'] = 'block';

  return $hooks;
}

This tells the system that when hook_block_info() and/or hook_block_view() are invoked, any [MODULE].block.inc files should be loaded. So you can then put these two hooks into [MODULE].block.inc, keeping your block code in a separate file altogether.

* Method 2 - different file for each block *

You can add module_load_include() to include files for each block. To do this, in hook_block_view() you can do the following:

function hook_block_view($delta = '')
{
  if($delta == 'block1')
  {
    module_load_include('inc', 'MODULENAME', 'MODULENAME.block1');
  }
  elseif($delta == 'block2')
  {
    module_load_include('inc', 'MODULENAME', 'MODULENAME.block2');
  }
}

With this code, you can now put block1 in MODULENAME.block1.inc and block2 in MODULENAME.block2.inc. Note that MODULNAME should be the actual name of your module, though technically the module name isn't required, it's just a good practice.

7
  • Thank you Brian, Could you give me an example? I can't make it work.
    – Joe
    Jan 5, 2017 at 5:44
  • Brian edited. I wrote it. Create a file named [MODULENAME].block.inc. Put it in the root of your module folder. Move those two functions you showed in your original post into that file. Put the code I gave into your .module file - but note that you have to rename the function according to hook naming methods.
    – Jaypan
    Jan 5, 2017 at 5:50
  • return $hooks; is missing, but still... the block is not appearing.
    – Joe
    Jan 5, 2017 at 5:53
  • Oops, I forgot the return. I edited my post accordingly. Did you clear your cache after making these changes? If so, then please tell us your module name, and the actual function names you are using for hook_hook_info(), hook_block_info(), hook_block_view(), and the name of the include file you created.
    – Jaypan
    Jan 5, 2017 at 6:01
  • Thank you Jaypan. The problem was the return. So, for every new block I need to add extra lines $hooks['block_info']['group'] = 'block2'; $hooks['block_view']['group'] = 'block2'; ? what is happening now, it is only creating the block2 instances. The idea is to create multiple blocks in different files, is that correct? Thanks.
    – Joe
    Jan 5, 2017 at 6:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.