Grid of terms in a block
This CodeLet show how to display terms in a vocabulary as block. The CodeLet will display the terms, along with term icon, if taxonomy image module is installed, in a grid of 4X4. The block is configurable. You can select which vocabulary to show the terms from. Its quite attractive and can be used on front page.
<?php
//$Id$
/**
* @file
*
* Display taxonomy tree in a block
*
* @author Drupal Developer
*/
/**
* Implementation of hook_block
*
* @param $op
* @param $delta
* @param $edit
* @return unknown_type
* @author Drupal Developer
*/
function browse_info_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[1] = array(
'info' => t("Browse information by category"),
'status' => 1,
'region' => 'content',
'weight' => 0,
);
return $blocks;
case 'configure':
$am__vocabulary = _get_vocabulary();
$am__imagecache = _get_imagecache();
$form['browse_vocabulary'] = array(
'#type' => 'select',
'#title' => t("Vocabulary"),
'#options' => $am__vocabulary,
'#default_value' => variable_get('browse_vocabulary_id', ''),
);
$form['imagecahce'] = array(
'#type' => 'select',
'#title' => t("Image size"),
'#options' => $am__imagecache,
'#default_value' => variable_get('imagecache_size', ''),
);
$form['image_size'] = array(
'#type' => 'textfield',
'#title' => t("Image size"),
'#size' => 30,
'#description' => t("Provide image size in WXH format.
This value will be ignored if a size already selected from above ImageCache presets."),
'#default_value' => variable_get('image_size', ''),
);
return $form;
case 'save':
switch ($delta) {
case 1:
variable_set('browse_vocabulary_id', $edit['browse_vocabulary']);
variable_set('imagecache_size', $edit['imagecahce']);
variable_set('image_size', $edit['image_size']);
break;
}
break;
case 'view':
switch ($delta) {
case 1:
$block = array('subject' => t("Browse information by category"), 'content' => get_category_tree());
break;
}
return $block;
}
}
/**
* get_category_tree
*
* Return the tree stucture
*
* @return unknown_type
* @author Drupal Developer
*/
function get_category_tree() {
global $conf;
$sn__vid = variable_get("browse_vocabulary_id", '');
$am__term_tree = array();
if (!$sn__vid) {
drupal_set_message("You need to select a vocabulary for the block.");
}
else {
$am__tree = taxonomy_get_tree($sn__vid, $parent = 0, $depth = -1, $max_depth = 2);
foreach ($am__tree as $om__term) {
if ($om__term->parents[0]) {
$am__term_tree[$om__term->parents[0]][] = $om__term->tid;
}
}
$ss__imagecache = variable_get('imagecache_size', '');
$ss__image_size = variable_get('image_size', '');
if (empty($ss__imagecache)) {
list($width, $height) = explode("X", $ss__image_size, -1);
$am__attribute = array('width' => $width, 'height' => $height);
}
$output .= "<div style='clear:both;width:100%;'>
<div style='float:left;width:100px;padding:3px;border:1px solid #DDD;margin:0px 8px 5px 0px;'>";
$i = 0;
$k = 1;
foreach ($am__term_tree as $key => $an__child) {
$j = 0;
if($key) {
if ($i == 4) {
$output .= ($key ? "</div></div><div style='clear:both;width:100%;'>
<div style='float:left;width:100px;padding:3px;
border:1px solid #DDD;margin:0px 8px 5px 0px;'>" : NULL);
$i = 0;
}
unset($om__term);
$ss__image = db_result(db_query("SELECT path FROM {term_image} WHERE tid = %d", $key));
$om__term = taxonomy_get_term($key);
if ($ss__imagecache) {
$ss__imagcache_name = db_result(db_query("SELECT presetname FROM
{imagecache_preset} WHERE presetid = %d", $ss__imagecache));
$output .= "<div>". theme('imagecache', $ss__imagcache_name,
variable_get('taxonomy_image_path', 'category_pictures') ."/". $ss__image) ."</div>";
}
else {
$output .= "<div>". theme('image',
variable_get('taxonomy_image_path', 'category_pictures')
."/". $ss__image, '', $am__attribute, FALSE) ."</div>";
}
$i++;
$output .= "<div><h4>". l($om__term->name,
drupal_get_path_alias("taxonomy/term/". $om__term->tid)) ."</h4></div>";
$output .= "<ul style='list-style:none;'>";
foreach ($an__child as $value) {
if ($j == 3) {
$output .= "</ul><div style='text-align:right;width:100px;
margin-bottom:10px;font-size:11px;padding: 0px 10px 0px 0px;'>".
l(t("More") ."...", drupal_get_path_alias("taxonomy/term/".
$om__term->tid)) ."</div>";
break;
}
else {
$om__child_term = taxonomy_get_term($value);
$output .= "<li style='font-size:11px;border-bottom:1px solid #EEE;'>". l($om__child_term->name, "");
}
?> (3 votes)
- Login or register to post comments
- 2791 reads
-
Your feedback


Recent comments
- I have contributed a module
21 weeks 11 hours ago - Improved Image menu
39 weeks 4 days ago - Great code!
41 weeks 2 days ago - Code Search is ready
50 weeks 5 days ago - I would love to see an
1 year 2 weeks ago - Thank you very much for your
1 year 4 weeks ago - Commented code
1 year 4 weeks ago - We are glad to know that the
1 year 4 weeks ago - Enhancements to codes
1 year 4 weeks ago - We have developed a module
1 year 4 weeks ago



Comments
Though this CodeLet is incomplete, its worth using it.
Its quite easy to extend this CodeLet and make it ready to use.
Thanks for publishing such a very useful CodeLet here.
Regards.
[url=http://joshics.in]JoshicsIN[/url]
Please note that this code is impcomplete.
This code will be updated soon.
Thank you.
[url=http://twitter.com/DrupalD][i]Drupa Developer - A Comprehensive Guild to Drupal Developersl[/i][/url]