I've been working on a custom module for a client that implements various hooks and modifies a few forms to add extra options. Everything has been going smoothly until I tried to implement hook_block(). I've only used hook_block() once before, but it worked as expected then. This time for some reason no matter what I specify for parameters, I do not see the block(s) I've created in admin/build/block.

I've tried everything, including copying the hook_block() implementation verbatim from "Pro-Drupal Development." Still no dice. I don't see how this could be a bug in Drupal or in the theme, but I am at a complete loss as to what I could be doing incorrectly. Here's my implementation:

function mymodule_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('Membership Executive'),
      );
			
      $blocks[1] = array(
        'info' => t('New Message Panel Block'),
      );

      return $blocks;
    case 'view':
      switch ($delta) {
        case 0:
          $block['subject'] = t('Membership Executive');
          $block['content'] = mymodule_block_content(0);
				
        case 1:
          $block['subject'] = t('New Message Panel Block');
          $block['content'] = mymodule_block_content(1);
      }
      
      return $block;
    }
}

The mymodule_block_content() function just returns some strings right now, just to get content inside the block. The strangest part about this dilemma is the fact that while neither block that I've defined shows up in the admin block list, the FIRST block ($block[0]) shows up in the Panel Module's contributed block list (when you go to add content to a pane), but $block[1] does not... Am I completely crazy or am I missing some glaringly obvious error? I've tried adding and subtracting every combination of parameters I can think of to no avail. According to the drupal block-module example and Pro-Drupal Dev. what is shown above should be all I need to get a block to display in the block list.

Any help is appreciated, as I'd very much like to be able to create custom blocks in this fashion. I'm beginning work on a project that will require several blocks created this way, so right now I feel like I'm at an impasse. All my modules are up to date (except admin menu and Drupal core, which is currently 6.10)

Comments

coreyp_1’s picture

It should work. Did you clear your cache after adding the code?

-Corey

geo_ego’s picture

I had the same problem with Chameleon theme. After I chenged the theme, my custom block appeared in the block list.

globalabhishek’s picture

Hi,

I had the same problem. I had created block in my custom module. It was displaying in one theme but not displaying in another theme. I had done all the things but failed.
Then i un-installed my custom module and re-install the module. Then all the blocks were displaying in all the themes. And everything is working file.

Abhishek
globalabhishek@gmail.com

reptilex’s picture

I searched for hours and thought that might be an 6.x issue, well it is still something you should check while using drupal 7. No matter how many times you clear the cache and activate or deactivate the block it will not show up. You have to deactivate->uninstall->clear cache and reactivate.

hongquan’s picture

Thank you very much!

corbacho’s picture

Thanks. It worked. (Drupal 7)

corbacho’s picture

Thanks. It worked. (Drupal 7)

johnhanley’s picture

I wasted about 15 minutes trying to figure out why my custom block wasn't showing up in the block list before wising up.

I think this is because of the way blocks are registered and cached in Drupal 7.

rumble’s picture

I had the same problem but uninstalling and clearing cache's etc would not solve it, I finally managed to get the block to show up as soon as a gave it a string value inside the array as a delta instead of enumerating them with integers. I don't understand why this is an issue but changes
blocks[0] = array(
to
blocks['some-title'] = array (
solved the problem with some themes being unable to recognize the block.

rlnorthcutt’s picture

I had a similar issue - couldn't get the block to show up at all! I finally changed the delta from "quick_inquiry" to "quick-inquiry", cleared the cache (again) and we are golden...

thanks!

regards,
Ron Northcutt
Directory of Technical Marketing, Acquia

mouize’s picture

Hi,

It could sounds stupid, but try to add a break on each case in your switch.

And yeah, do not use integer keys but unique strings, it's better.

sr631’s picture

Has anyone found the cause/fix for this? I have a couple modules that create blocks - some show up, some don't. I'm using string deltas, and have cleared my cached 100s of times.

Any other suggestions?

Ank.g9’s picture

hii

after deactivate your modules you have to uninstall those modules and then clear cache and again reactivate your modules.

just follow the steps :-

deactivate->uninstall->clear cache and reactivate.

skoybrina’s picture

I change the name of the module,because there was the 'block' word in it,and it works :)

phreestilr’s picture

Same here. Removed the word "block' from the delta, and it appears now.

tahiche’s picture

Same here... very frustrating.

sstedman’s picture

In my case, uninstalling the module would be laborious. Being a dependency for many other modules I would first need to disable all of the dependencies then uninstall reinstall, then reenable the dependencies. Then would require following the same steps on a dev and live site.

I ran update.php and the block finally showed up in the admin list.

Pradslion’s picture

Code seems to be alright!!! Try disabling your module followed by uninstalling and reinstalling the same. Also you may clear cache on top of that.

golubovicm’s picture

Tried everything, but block just won't appear. Strange thing is that I'm adding block from module which already is registering 2 other blocks. Those 2 are working well. I can change their names, content and all that is working well. But when I try to add new block in identical way as those 2 are added it just won't appear. Tried disabling/enabling module, uninstalling it, calling update.php cleared cache zillion times. New block just wont' appear in blocks list.

anthonyf’s picture

I had trouble with getting a custom block to appear in the D7 Blocks page list as disabled, while other custom blocks that had been assigned a theme region were appearing as expected. It finally appeared after setting the region to -1 in the hook_block_info() function as follows:

  $blocks['callout-block'] = array(
    // The name that will appear in the block list.
    'info' => t('Callout Block'),
    // Default setting.
    'cache' => DRUPAL_CACHE_PER_ROLE,
    'status' => 1,
    // For block to be listed as disabled on blocks page, set region to -1.
    'region' => -1,
    'theme' => 'my_theme',
  );

And to get the above block to function correctly - so that I could assign it to a region via the admin interface and it would actually show up - I had to change the $block array key from callout_block to callout-block.

golubovicm’s picture

Tried, but didn't help. Thanks anyway.

Mohammed H’s picture

To appear your custom block in the D7 add _info like :

    function my_bname_block_info() { 
        $block = array();
        $block['my_bname_block']['info'] = t('Block name');

       

        return $block;

    }

function my_bname_block_view($delta = '') {

       // add your code here ...

}