0

I've created a drupal module, it's info file, and a tpl.php for the block all in the same module directory. When I enable the module, I can see the block listed and assign it to a region. However when I view the page the block does not display. If I do a vardump on $content within block_view, the dump displays at the top of the page, and firebug shows that block is wrapped with the right div tags. I would think if you did a var dump on within the block_view function, it should display in the right region. This one is probably really simple, but it's got me stumped - any help much appreciated. Also if I title the block just to see if it shows up anywhere in the page - it's still nowhere to be seen even though admin shows it it in the region, and no filters are set to prevent it being viewed. I would think that if you gave the block a title -if it's on the page it would at least show a title even if everything else is blank.

module file:

function my_block_block_info() {
   $blocks['my_block'] = array(
      'info' => t('My Block')
   );
   return $blocks;
}

function my_block_block_view() {

if(arg(0) == 'node' && is_numeric(arg(1)))  {
    $id = arg(1);
}     
   $query = "SELECT * FROM table WHERE id= :id AND deleted=0";
   $args = array(':id' => $id);  
   $result = db_query($query,$args)->fetchAll();
   foreach($result as $item) {
     $stuff[] = array($item->stuff_url,$item->stuff_name);
   }
   $content=
      theme_render_template(drupal_get_path('module','my_module').'/'.'block--         my_block.tpl.php',array("stuff" => $stuffs));
   return $content;
   }

 block--my_block_tpl.php

<div class="wrapper">
 <?php      

 foreach($stuffs as $thing) {
 for($i=0; $i < count(thing); $i++) {
       //print out result passed from module
  } 
  }
 ?>
 </div>
1
  • check if you have cleared the cache (especially if you have apc or something like that) check if you have privillege to see the block. Check for any css or js that might hide the block.. see your php log and dblog for any other clues.
    – NenadP
    Commented Mar 20, 2013 at 7:05

2 Answers 2

1

your hook_block_view() is implemented in a wrong way,

  1. first pass in argument delta as a parameter
  2. you should put the contents of your block inside the $block['content'] variable.
  3. return $block;

heres a very good tutorial building a custom block

1
  • Thanx! That helped!
    – user14803
    Commented Mar 21, 2013 at 0:34
0

use your query as drupal standard like this way

$query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'created'))
    ->condition('status', 1) //Published.
    ->orderBy('created', 'DESC') //Most recent first.
    ->execute(); 
  return $query;  
}

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.