1

I am new to Drupal. I tried to write custom module(Sample).. After completion of Module, Enabled at Module. But it is not showing at Block Page.

Please check the below code and correct me..

first.info

;$Id$

name = First
description = A first module.
package = Drupal 7 Development
core = 7.x
files[] = first.module

;dependencies[] = autoload
;php = 5.2

first.module

function first_block($op = 'list', $delta = 0, $edit = array()){
    switch ($op) {
       case 'list':
        $block = array();
        //List out all blocks you want to create here
        $block[0]["info"] = t('Display info'); 
        return $block; 
       case 'view':    
        switch ($delta) {
          case 0:
            $block['subject'] = "ADMIN Header of the block";
            global $user;
            if(in_array('Site admin', array_values($user->roles) || $user->uid == 1)) {
              $block['content'] = "input form";
              $block['subject'] = "Header of the block";
            }    
            return $block;
        }
    }           
}
  • Is this the source code of your .module file ? – niksmac Nov 26 '12 at 8:42
  • 2
    Can you confirm which Drupal version you're using? It's been tagged as 7 (and your info file says 7), but the block implementation code is for 6. I'm pretty sure you mean 7, but I don't want to assume anything :) – Chapabu Nov 26 '12 at 9:11
  • I am using Drupal 7.. – Kishore Nov 26 '12 at 9:18
  • 2
    @Kishore You have implemented Drupal 6 hook in Drupal 7. Please check out all block related hooks for Drupal 7.. The main difference between these two is that all cases inside switch statement is now separate hooks in 7. – GoodSp33d Nov 26 '12 at 9:29
  • 1
    @2-Stroker - You should write this as an answer :) – Chapabu Nov 26 '12 at 9:58
2

Did you try to (steps in order):

  1. use a minimal hook_block_info() definition for your block (such as the one below or even without 'cache' option!) the very first time?
  2. manually remove your block's entries in block table, if any?
  3. then clean all caches?

Longer version of why these steps may help:

Using a minimal block definition such:

function sso_block_info() {
  $blocks['applinks'] = array(
    'info'   => t('Single Sign On'),
    'cache'  => DRUPAL_NO_CACHE, // this can also be left out!
  );
  return $blocks;
}

Would not list my block in the "Disabled" pseudo-region of admin/structure/block!

Troubleshooting revealed that block had been inserted in block table, but its value for the region column was blank and not the usual '-1' that every other currently disabled block contained!

Tip: updating this value to -1 directly within DB made block 'Single Sign On' show up in block admin's Disabled zone instantly!

Then I manually removed my block's entries from DB and cleaned caches to effect Drupal calling again hook_block_info. This time my block showed up correctly in block admin UI the first time!

My hunch is that existing config entries in DB for blocks will sometimes interfere with what is indeed correct block registration code! Cache clearing will not affect entries in block table! I guess that certain perhaps invalid combinations of options in hook_block_info() will cause D7 to register a block with a blank value for the region (a block appears in) instead of -1 (a disabled block).

| improve this answer | |
0

Add this line to your hook_block.

 global $user;

The $user variable cannot be accessible within a function without declaring as global.
Thus, if you logged in as administrator, your condition will not work and your block has no content and subject.

 if(in_array('Site admin', array_values($user->roles) || $user->uid == 1)){
          $block['content'] = "input form";
          $block['subject'] = "Header of the block";
 }

Regardless of Drupal 6 or 7, you must declare $user as global so that you can use it in the functions (hooks).

| improve this answer | |
  • Sithu, Thanks for your Quick response, But still i am not getting this... – Kishore Nov 26 '12 at 9:13
  • Try to clear cache. – Sithu Nov 26 '12 at 9:16
  • @Sithu OP is using Drupal 7 and using hook_block which is of Drupal 6 – niksmac Nov 26 '12 at 9:57

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.