Hi here is the code that works when i put it in a custom block as 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 trying to convert it to proper Drupal Block created through a module here is my .module code:
<?php
/**
* @file
* Archive Accordion will give you a new block for the blog content type.
*/
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function archive_accordion_help($path, $arg) {
switch ($path) {
case "admin/help#archive_accordion":
return '<p>'. t("Archive Accordion will give you a new block for the blog content type") .'</p>';
break;
}
}
/**
* Implements hook_block_info().
*/
function archive_accordion_block_info() {
$blocks['archive_accordion'] = array(
'info' => t('Archive Accordion'), //The name that will appear in the block list.
'cache' => DRUPAL_CACHE_PER_ROLE, //Default
);
return $blocks;
}
/**
* Custom content function.
*
* @return
* A result set of the targeted posts.
*/
function archive_accordion_contents(){
//Later fix Use Database API to retrieve current posts.
/*
$query = db_select('node', 'n')
->fields('n', array('nid', 'title', 'created'))
->condition('status', 1) //Published.
->condition('created', array($start_time, $end_time), 'BETWEEN')
->orderBy('created', 'DESC') //Most recent first.
->execute();
return $query;
*/
$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");
}
/**
* Implements hook_block_view().
*
* Prepares the contents of the block.
*/
function archive_accordion_block_view($delta = '') {
switch($delta){
case 'archive_accordion':
$block['subject'] = t('Archive Accordion');
if(user_access('access content')){
//Use our custom function to retrieve data.
$result = archive_accordion_contents();
//Array to contain items for the block to render.
$items = array();
//Iterate over the resultset and format as links.
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";
if (empty($items)) { //No content.
$block['content'] = t('No posts available.');
}
else {
//need to fix to Pass data through theme function.
$block['content'] = $output;
}
}
}
return $block;
drupal_add_library('system', 'ui.accordion');
drupal_add_js('jQuery(document).ready(function(){jQuery("#archivetree").accordion({ active: "h3:last" });});', 'inline');
}
and i am getting the following errors on my page:
Warning: Invalid argument supplied for foreach() in archive_accordion_block_view() (line 79 of /whatever/sites/all/modules/archive_accordion/archive_accordion.module).
Notice: Undefined variable: archive_tree in archive_accordion_block_view() (line 95 of /whatever/sites/all/modules/archive_accordion/archive_accordion.module).
Warning: Invalid argument supplied for foreach() in archive_accordion_block_view() (line 95 of /whatever/sites/all/modules/archive_accordion/archive_accordion.module).
Notice: Undefined variable: output in archive_accordion_block_view() (line 126 of /whatever/sites/all/modules/archive_accordion/archive_accordion.module).
Any help is really appreciated... :)