2

I have a module that is getting pretty large and I want to move the functions that define blocks into another file to better organize my module.

I did this:

  1. Moved those functions ( _block_configure, _block_info, _block_view, etc) to includes/blocks.inc in my module.
  2. Put files[] = includes/blocks.inc in my .info file

This doesn't work though - the blocks disappear when I do this. What am I doing wrong?

6

The files directive is used to include files containing classes, check documentation. You could include the file in the .module file.

require_once __DIR__ . '/includes/blocks.inc';

Edit: Included the other solutions added by @Beebee and @Jimajamma in the answer:

require_once  drupal_get_path('module', 'mymodulename') . '/includes/blocks.inc';
module_load_include('inc', 'MY_MODULE', 'FILE_NAME');
// This method is including a file residing in the module folder not in a subdirectory.
2

All you need is hook_hook_info.

Normally hooks do not need to be explicitly defined. However, by declaring a hook explicitly, a module may define a "group" for it. Modules that implement a hook may then place their implementation in either $module.module or in $module.$group.inc. If the hook is located in $module.$group.inc, then that file will be automatically loaded when needed. In general, hooks that are rarely invoked and/or are very large should be placed in a separate include file, while hooks that are very short or very frequently called should be left in the main module file so that they are always available.

In your case it should be,

/**
* Implements hook_hook_info().
*/
function mymodule_hook_info() {
  $hooks['block_info'] = array(
    'group' => 'block',
  );
  $hooks['block_configure'] = array(
    'group' => 'block',
  );
  $hooks['block_view'] = array(
    'group' => 'block',
  );
  return $hooks;
}

Then you should place those hook functions into a file called mymodule.block.inc and the same file should be available in the module directory.

For more info/caveats on this hook go here.

  • hook_hook_info sounds great, but if you dig a little deeper, you'll see the limitations. Try to do that block thing in two modules and you'll see, that it won't work anymore. This is because the info provided by using this hook, is aggregated over all modules (see module_hook_info), so that instead of 'group' => 'blocks' you'll end up with 'group' => array(0 => 'block', 1 => 'block'). This will fail as you can immediately see on the block page. I would use this with extreme caution. – berliner Sep 9 '14 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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