WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Create a custom block for Drupal with a module using hook_block

Submitted by barnettech on Sat, 01/02/2010 - 10:55


Replace the corresponding function above with whats below to get recent posts in your drupal block



function latest_block($op='list',$delta=array(),$edit=array())
{
      switch ($op)
     {

        case 'list':
                       $blocks[0]['info'] = t('Latest CTL Content');
                       return $blocks;
                       break;
         case 'view':
           ob_start();
         $num_nodes = 5;

$result = db_query_range(db_rewrite_sql('SELECT n.nid, n.title, n.type FROM {node} n WHERE n.status = 1 AND n.type <> "forum" ORDER BY n.changed DESC'), 0, $num_nodes); 

$output = node_title_list($result);
$output .= l('More...', 'tracker');
          //$content = 'hello world';
          $content = $output;
          $blocks['subject'] = t('Latest CTL Content');
          $blocks['content'] = $content;
       return $blocks;
       break;
      }
}

And here is an example with multiple blocks in the same module:



function latest_block($op='list',$delta=array(),$edit=array())
{
      switch ($op)
     {

        case 'list':
                       $blocks[0]['info'] = t('Latest CTL Content Block');
                       $blocks[1]['info'] = t('Partner Block');
                       return $blocks;
                       break;
         case 'view':
           switch ($delta) {
           case 0:
              //ob_start();
              $num_nodes = 2;

              $result = db_query_range(db_rewrite_sql('SELECT n.nid, n.title, n.type FROM {node} n WHERE n.status = 1 AND n.type = "Biography" ORDER B
Y n.changed DESC'), 0, $num_nodes);
              $result2 = db_query_range(db_rewrite_sql('SELECT n.nid, n.title, n.type FROM {node} n WHERE n.status = 1 AND n.type = "page" ORDER BY n.
changed DESC'), 0, $num_nodes);
              $result3 = db_query_range(db_rewrite_sql('SELECT n.nid, n.title, n.type FROM {node} n WHERE n.status = 1 AND n.type = "news" ORDER BY n.
changed DESC'), 0, $num_nodes);
 
              $output = node_title_list($result);
              $output .= node_title_list($result2);
              $output .= node_title_list($result3);
//$output .= l('More...', 'tracker');
              //$content = 'hello world';
              $content = $output;
              $blocks['subject'] = t('Latest CTL Content');
              $blocks['content'] = $content;
              //return $blocks;
              break;
           case 1:
              $blocks['subject']=t('Parnter Block');
              $blocks['content']="put the link to the content here";
              break;

         }
         return $blocks;


}
}



also check out http://drupal.org/node/60528