Drupal: Taxonomy tree with image

This CodeLet creates a tiled dispay of taxonomy block with child terms. If you use taxonomy image module, you can select the preset and taxonomy on block configuration page. The CodeLet then fetches all the tree along with their images and display in tile in the block.

CodeLet: 


/**
* @file
*
* Display taxonomy tree in a block
*
* @author DrupalD
*/

/**
* Implementation of hook_block
*
* @param $op
* @param $delta
* @param $edit
* @return unknown_type
* @author DrupalD
*/
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(
'' => 'select',
'' => t("Vocabulary"),
'' => $am__vocabulary,
'' => variable_get('browse_vocabulary_id', ''),
);
$form['imagecahce'] = array(
'' => 'select',
'' => t("Image size"),
'' => $am__imagecache,
'' => variable_get('imagecache_size', ''),
);
$form['image_size'] = array(
'' => 'textfield',
'' => t("Image size"),
'' => 30,
'' => t("Provide image size in WXH format.
This value will be ignored if a size already selected from above ImageCache presets."),
'' => 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 DrupalD
*/
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 .= "
#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 ? "
#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 .= "". theme('imagecache', $ss__imagcache_name,
variable_get('taxonomy_image_path', 'category_pictures') ."/". $ss__image) ."";
}
else {
$output .= "". theme('image',
variable_get('taxonomy_image_path', 'category_pictures')
."/". $ss__image, '', $am__attribute, FALSE) ."";
}
$i++;
$output .= "". l($om__term->name,
drupal_get_path_alias("taxonomy/term/". $om__term->tid)) ."";
$output .= "

    ";
    foreach ($an__child as $value) {
    if ($j == 3) {
    $output .= "

".
l(t("More") ."...", drupal_get_path_alias("taxonomy/term/".
$om__term->tid)) ."";
break;
}
else {
$om__child_term = taxonomy_get_term($value);
$output .= "

  • #EEE;'>". l($om__child_term->name,
    drupal_get_path_alias('taxonomy/term/'. $om__child_term->tid))
    ."
  • ";
    $j++;
    }

    }
    $output .= "

    ";
    $output .= (($i < 4 && !empty($an__child)) ?
    "#DDD;margin:0px 8px 5px 0px;'>" : NULL);
    }
    }
    $output .= "";
    $output .= "&nbsp";
    return $output;
    }
    }

    /**
    * _get_vocabulary
    *
    * Helper function. Return list of vocabularies
    * @return unknown_type
    * @author DrupalD
    */
    function _get_vocabulary() {
    $r__result = db_query("SELECT vid, name FROM {vocabulary}");
    while($om__result = db_fetch_object($r__result)) {
    $am__vocabulary[$om__result->vid] = $om__result->name;
    }
    return $am__vocabulary;
    }

    /**
    * _get_imagecache
    *
    * Return the imagecache preset
    *
    * @return unknown_type
    * @author DrupalD
    */
    function _get_imagecache() {
    $r__result = db_query("SELECT * FROM {imagecache_preset}");
    $am__preset[''] = t("Select");
    while ($om__result = db_fetch_object($r__result)) {
    $am__preset[$om__result->presetid] = $om__result->presetname;
    }
    return $am__preset;
    }

    Drupal: Taxonomy tree with image | Drupal Developer

    By DrupalD on Fri, 26/04/2013 - 12:45pm

    Add comment

    Log in or register to post comments
    QR code for this page URL This page URL
    Feedback