1

I'm working on a module which renders a small block in a footer region. My goal is to make it work out of the box with default settings. User just needs to enable it with drush, no configuration required.

In Drupal 6 version I stumbled upon a strange problem. Looks like after enabling module it's hook_block isn't called, so block isn't rendered. It can be fixed either by doing drush cc all or going to blocks admin page. All caching is disabled on the test site.

I tried to incorporate drupal_flush_all_caches and drupal_cache_clear into hook_install without success.

Can someone give a hint?

3

After some trials I found an easy workaround:

function mymodule_enable() {
  _block_rehash();
}

It's function which get called then one visits /admin/build/block. It rescans blocks provided by modules.

  • 1
    Always helpful to link to the API for that function api.drupal.org/api/drupal/modules--block--block.module/function/… – mikeytown2 Jan 17 '12 at 5:37
  • 1
    I have used _block_rehash() in the past to force the block to show up at module install time. If you follow code paths, when you browse to /admin/block, the block_admin_display() function is called, which contains _block_rehash() inside of that function. block_admin_display(), does not get called when a module is installed that contains a hook_block(). Rafaelcr's suggestion should also work to force the block to load without manually browsing to /admin/block. – Mike Munroe Jan 20 '12 at 19:33
2

You need to implement the hook_install hook in an .install file for your module. You'll write a record to the blocks table to add your specified block to a certain region on a certain theme whenever you enable the module. You'll use the drupal_write_record function to write an array like the following (or you could just use an INSERT query, although deprecated):

$block = array(
  'module' => 'module_name', // your module name
  'delta' => 0, // the delta of your block in hook_block
  'theme' => 'theme_name', // The name of the theme you'll add the block to
  'region' => 'footer', // The system name of the region you'll add it to
  'status' => 1,
);
  • What I don't understand, is why I need to manually add a record into a table. hook_block already returns status and region parameters, why they are not taken into account until I go to admin/blocks? – Dmitry Vyal Jan 16 '12 at 10:08

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.