You are here

Create a custom block for Drupal with a module using hook_block

Wiki Terms: 

<?php

// $Id$

/**
 * A module to create a basic block to be available from the drupal blocks menu page
 *
 *
 *
 * 
 */




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':
   

          $content = 'hello world';
          $blocks['subject'] = t('Latest CTL Content');
          $blocks['content'] = $content;
       return $blocks;
       break;
      }
}


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

Comments

Thanks for this great tutorial. Simple, to the point, but clear enough for those of us with only rudimentary PHP skills.

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer