2

I want to build a "Blog Archive" block with a custom module. How do I do it?

4
  • create a new folder in /sites/default/modules
  • create a custom_blocks.info file inside that folder
  • add this content in the file:
name = Custom blocks
description = "A short description of your module"
package = My modules
files[] = custom_blocks.module
core = 7.x
  • create another file: custom_blocks.module (note that you've specified this in the info file)
  • add this PHP code:
<?php
  function custom_blocks_block_info() {
    $blocks = array();

    $blocks['unique_id_for_your_block'] = array(
      'info' => t('block info'),
      'cache' => DRUPAL_NO_CACHE,
    );

    // You can add more blocks here.

    return $blocks;
  }

  function custom_blocks_block_view($delta = '') {
    $block = array();

    switch ($delta) {
      case 'unique_id_for_your_block':

        // Put your logic here and generate the HTML you want to display in your block.
        $block['subject'] = t('block title');
        $block['content'] = 'Put here the HTML generated above';
        break;

      // Add more case statements here if you defined more than one block.

    }

    return $block;
  }
  • 1
    The files should be placed in /sites/default/modules/custom_blocks, rather than /sites/default/modules. – kiamlaluno May 25 '11 at 19:20
1

Hi this solution is not "drupal way" way of doing this, but it does the job. Here is the code that needs to be put into a custom block where and input type needs to be PHP.

<?php

drupal_add_library('system', 'ui.accordion');
drupal_add_js('jQuery(document).ready(function(){jQuery("#archivetree").accordion({ active: "h3:last" });});', 'inline');

$result = db_query("SELECT DATE_FORMAT((DATE_ADD('19700101', INTERVAL node.created SECOND) + INTERVAL -18000 SECOND), '%Y%m') AS created_year_month, COUNT(node.nid) AS num_records
FROM 
{node} node
WHERE (( (node.status = '1') AND (node.type IN  ('blog')) ))
GROUP BY created_year_month
ORDER BY created_year_month ASC");


foreach ($result as $record) {

    $created_year_month = $record->created_year_month;
    $num_records = $record->num_records;
    $year =  substr($created_year_month, 0, 4);
    $month = substr($created_year_month, -2, 2);
    $month_name = date( 'F', mktime(0, 0, 0, $month) );

    $archive_tree[$year][$month] = array(
        'month'            => $month,
        'name'             => $month_name,
        'num_records'      => $num_records,
        'year'             => $year,
    );
}
$i_year = 0;
foreach ($archive_tree as $list_year) {
    if ($i_year == 0){
        $output = "<div id='archivetree'>\n";
        $i_year++;
    }

        $i_month = 0;
        foreach ($list_year as $list_month) {

            if ($i_month == 0){
                $output .= "<h3><a href='#'>";
                $output .= $list_month['year'];
                $output .= "</a></h3>\n";
                $output .= "<ul>\n";
                $i_month++;
            }

            $text = $list_month['name'];
            $text .= " (";
            $text .= $list_month['num_records'];
            $text .= ")";
            $path = "blog/archive/";
            $path .= $list_month['year'];
            $path .= "/";
            $path .= $list_month['month'];
            $link = l($text,$path);
            $output .= "<li>$link</li>\n";          
        }
        $output .= "</ul>\n";

}
        $output .= "</div>\n";
print $output;

?>

I am currently working on implementing this as a custom module, but am not quite there yet.

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.