You are here

taxonomy_block.module in Taxonomy Block 7

Same filename and directory in other branches
  1. 5 taxonomy_block.module
  2. 6 taxonomy_block.module

The taxonomy_block module.

File

taxonomy_block.module
View source
<?php

/**
 * @file
 * The taxonomy_block module.
 */

/************************************************************************************
 *                  CONFIG
 ***********************************************************************************/
define('TAXONOMY_BLOCK_PERM_ACCESS', 'access taxonomy_block');
define('TAXONOMY_BLOCK_PERM_ADMIN', 'administer taxonomy_block');

/************************************************************************************
 *                  DRUPAL HOOKS 
 ***********************************************************************************/

/**
 * Implementation of hook_help();
 */
function taxonomy_block_help($section) {
  switch ($section) {
    case 'admin/help#taxonomy_block':
      return '<p>' . t('Displays taxonomy hierarchy in a block.') . '</p>';
    case 'admin/modules#description':
      return 'Displays taxonomy hierarchy in a block.';
  }
}

/**
 * Implementation of hook_permission();
 */
function taxonomy_block_permission() {
  return array(
    TAXONOMY_BLOCK_PERM_ACCESS => array(
      'title' => t('Access taxonomy block'),
      'description' => t('Read access for taxonomy block.'),
    ),
    TAXONOMY_BLOCK_PERM_ADMIN => array(
      'title' => t('Administer taxonomy block'),
      'description' => t('Administration access for taxonomy block.'),
    ),
  );
}

/**
 * Implementation of hook_menu();
 */
function taxonomy_block_menu() {
  $items = array();
  $items['admin/config/taxonomy_block'] = array(
    'title' => 'Taxonomy Block',
    'description' => 'Displays taxonomy hierarchy',
    'access arguments' => array(
      TAXONOMY_BLOCK_PERM_ADMIN,
    ),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'taxonomy_block_admin_settings',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'taxonomy_block.settings.inc',
  );
  return $items;
}

/**
 * Implementation of hook_block_info();
 */
function taxonomy_block_block_info() {
  $blocks['taxonomy_block'] = array(
    'info' => t('Taxonomy Block'),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implementation of hook_block_view();
 */
function taxonomy_block_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'taxonomy_block':
      if ($vid = variable_get('taxonomy_block_vid', 0)) {
        $block['subject'] = taxonomy_vocabulary_load($vid)->name;
        $block['content'] = taxonomy_block_generate_list(taxonomy_block_build_taxonomy_tree($vid, 0), (bool) variable_get('taxonomy_block_node_count', 0));
      }
      break;
  }
  return $block;
}

/************************************************************************************
 *                  API
 ***********************************************************************************/

/**
 * Generates & returns a nested array of taxonomy terms for the specified vocabulary id;
 * @return 
 * @param object $vid
 * @param object $parent[optional]
 * @param object $max_depth[optional]
 */
function taxonomy_block_build_taxonomy_tree($vid, $parent = 0, $max_depth = NULL) {
  static $results;
  $args = func_get_args();
  $hash = md5(implode('-', $args));
  if (!isset($results[$hash])) {
    $terms = array();
    $data = taxonomy_get_tree($vid, $parent, $max_depth);
    foreach ($data as $term) {
      $terms[$term->tid] = $term;
    }
    $terms = taxonomy_block_nest_taxonomy_terms($terms, $parent);
    $results[$hash] = $terms;
  }
  return $results[$hash];
}

/**
 * Looks for each term's parent term in the given tree and assigns 
 * the child to the parent.
 * @return 
 * @param object $terms
 * @param object $parent
 */
function taxonomy_block_nest_taxonomy_terms($terms, $parent) {
  foreach ($terms as $term) {
    $parent_tid = isset($term->parents[0]) ? $term->parents[0] : 0;
    if ($parent_tid) {
      if (isset($terms[$parent_tid])) {
        $terms[$parent_tid]->children[$term->tid] = $term;
        unset($terms[$term->tid]);
      }
      elseif ($parent_tid != $parent) {
        taxonomy_block_nest_taxonomy_terms_child($terms, $parent_tid, $term);
        unset($terms[$term->tid]);
      }
    }
  }
  return $terms;
}

/**
 * Recursive function which nests the child element within the appropriate
 * parent element in the given terms tree.
 * @return 
 * @param object $terms
 * @param object $parent_tid
 * @param object $child
 */
function taxonomy_block_nest_taxonomy_terms_child(&$terms, $parent_tid, $child) {
  foreach ($terms as &$term) {
    if ($term->tid == $parent_tid) {
      $term->children[$child->tid] = $child;
      break;
    }
    elseif (!empty($term->children)) {
      taxonomy_block_nest_taxonomy_terms_child($term->children, $parent_tid, $child);
    }
  }
}

/**
 * Generates a nested HTML list from hierarchy items.
 * @return string $output
 * @param array $hierarchy
 * @param bool $show_counts[optional]
 */
function taxonomy_block_generate_list($hierarchy, $show_counts = FALSE) {
  $items = array();
  foreach ($hierarchy as $term) {
    $title = $term->name;
    if ($show_counts) {
      $title .= t(' (@count)', array(
        '@count' => taxonomy_block_count_nodes_term($term->tid),
      ));
    }
    $item = l($title, 'taxonomy/term/' . $term->tid);
    if (!empty($term->children)) {
      $item .= taxonomy_block_generate_list($term->children, $show_counts);
    }
    $items[] = $item;
  }
  return theme('item_list', array(
    'items' => $items,
  ));
}

/**
 * Counts the number of nodes assigned to a term.
 * @return integer $count - number of nodes
 * @param integer $tid - term id
 */
function taxonomy_block_count_nodes_term($tid) {
  return db_select('taxonomy_index', 'ti')
    ->condition('tid', (int) $tid)
    ->countQuery()
    ->execute()
    ->fetchField();
}

Functions

Namesort descending Description
taxonomy_block_block_info Implementation of hook_block_info();
taxonomy_block_block_view Implementation of hook_block_view();
taxonomy_block_build_taxonomy_tree Generates & returns a nested array of taxonomy terms for the specified vocabulary id;
taxonomy_block_count_nodes_term Counts the number of nodes assigned to a term.
taxonomy_block_generate_list Generates a nested HTML list from hierarchy items.
taxonomy_block_help Implementation of hook_help();
taxonomy_block_menu Implementation of hook_menu();
taxonomy_block_nest_taxonomy_terms Looks for each term's parent term in the given tree and assigns the child to the parent.
taxonomy_block_nest_taxonomy_terms_child Recursive function which nests the child element within the appropriate parent element in the given terms tree.
taxonomy_block_permission Implementation of hook_permission();

Constants