You are here

function taxonomy_list_get_tree in Taxonomy List 5.2

Same name and namespace in other branches
  1. 6.2 taxonomy_list.module \taxonomy_list_get_tree()
  2. 7 taxonomy_list.module \taxonomy_list_get_tree()

Returns tree of terms. Enhancement for taxonomy_get_tree.

Parameters

$parent: the parent term to restrict the tree to. (optional)

$overview: whether we are doing the overview page (bool)

Return value

an array of term objects.

2 calls to taxonomy_list_get_tree()
taxonomy_list_block in ./taxonomy_list.module
Implementation of hook_block().
taxonomy_list_show in ./taxonomy_list.module
Show the category list

File

./taxonomy_list.module, line 113
List all terms in a vocabulary.

Code

function taxonomy_list_get_tree($vocid, $parent = 0, $max_depth = 2147483647) {
  $taxo_img = module_exists('taxonomy_image');
  $show_image = variable_get('taxonomy_list_show_image', 1);
  $count_type = variable_get('taxonomy_list_count', 'none');
  $no_show = variable_get('taxonomy_list_noshow', FALSE);
  $edit_link = $op == 'block' ? FALSE : variable_get('taxonomy_list_edit_link', FALSE);
  $search_link = $op == 'block' ? FALSE : variable_get('taxonomy_list_search_link', FALSE);
  $rss_link = variable_get('taxonomy_list_rss_link', FALSE);
  $show_parents = variable_get('taxonomy_list_show_parents', FALSE);
  $show_children = variable_get('taxonomy_list_show_children', FALSE) ? '/all' : NULL;
  $related = variable_get('taxonomy_list_related', FALSE);
  $synonyms = variable_get('taxonomy_list_synonyms', FALSE);
  $ntf_avail = module_exists('node_type_filter');
  $destination = drupal_get_destination();
  $tree = taxonomy_get_tree($vocid, $parent);
  $vocabulary = taxonomy_vocabulary_load($vocid);

  // Is this a top level request?
  if ($parent) {

    // The existing elements have depths one too low.
    foreach ($tree as $term) {
      ++$term->depth;
    }

    // Not top level, so we need to get the requested term
    // and stick it on the front of the tree.
    $parent_term = taxonomy_get_term($parent);
    array_unshift($tree, $parent_term);
    $tree[0]->depth = 0;
  }
  $new_tree = array();
  foreach ($tree as $term) {
    $tid = $term->tid;

    // If we are too deep already, skip the whole term.
    if ($term->depth > $max_depth) {
      continue;
    }

    // If we are suppressing empty terms and there are no links in this group, skip it.
    $count = taxonomy_term_count_nodes($tid);
    if ($no_show && $count == 0) {
      continue;
    }
    $new_tree[$tid] = $term;
    $term_path = drupal_get_path_alias(taxonomy_term_path($term) . $show_children);
    $new_tree[$tid]->children = array();
    if ($term->parents[0] != 0) {
      foreach ($term->parents as $parent) {
        if (isset($new_tree[$parent])) {
          $new_tree[$parent]->children[] = $tid;
        }
      }
    }
    if ($show_image) {
      $new_tree[$tid]->image = $taxo_img ? '<div class="taxonomy-list-image">' . taxonomy_image_display($term->tid, NULL, NULL, array(
        'wrapper' => FALSE,
      )) . '</div>' : NULL;
    }
    else {
      $new_tree[$tid]->image = NULL;
    }
    $new_tree[$tid]->title = '<div class="taxonomy-list-title">' . l($term->name, $term_path, array(
      'attributes' => array(
        'id' => $term->tid,
      ),
    )) . '</div>';
    $new_tree[$tid]->desc = $term->description ? '<div class="taxonomy-list-desc">' . check_markup($term->description, $format) . '</div>' : NULL;
    $links = array();

    // Do we want edit link?
    if (user_access('administer taxonomy') && $edit_link) {
      $links['taxonomy-list-edit-link'] = array(
        'title' => t('edit term'),
        'href' => 'admin/content/taxonomy/edit/term/' . $term->tid,
        'attributes' => array(
          'title' => t('make changes to this term'),
        ),
        'query' => $destination,
      );
    }

    // Do we want search link?
    if (user_access('search content') && $search_link) {
      $links['taxonomy-list-search-term'] = array(
        'title' => t('search for term'),
        'href' => 'search/node/"' . $term->name . '"',
        'attributes' => array(
          'title' => t('search for content using this term'),
        ),
      );
    }

    // Do we want RSS link?
    if ($rss_link) {
      $links['taxonomy-list-rss'] = array(
        'title' => '<img src="' . base_path() . 'misc/feed.png" alt="rss feed for ' . check_plain($term->name) . '" />',
        'href' => 'taxonomy/term/' . $term->tid . '/' . $controls['max_depth'] . '/feed',
        'attributes' => array(
          'title' => t('create feed for this term'),
        ),
        'html' => TRUE,
      );
    }
    if ($links) {
      $new_tree[$tid]->links = theme('links', $links, array(
        'class' => 'links inline',
      ));
    }
    switch ($count_type) {
      case 'none':
        $counter = NULL;
        break;
      case 'all':
        $count = taxonomy_term_count_nodes($term->tid);
        if ($count == 0 && $no_show) {
          return NULL;
        }
        $counter = '<div class="taxonomy-list-term-count">(' . $count . ')</div>';
        break;
      case 'not_zero':
      case 'by_type':
        $count_list = array();
        $count = 0;
        foreach ($vocabulary->nodes as $type) {
          $this_count = taxonomy_term_count_nodes($term->tid, $type);
          if ($this_count > 0 || $count_type == 'by_type') {

            // Is Node Type Filter available?
            if ($ntf_avail && $this_count > 0) {
              $count_list[] = l($type . ': ' . $this_count, $term_path, array(
                'query' => 'type=' . $type,
              ));
            }
            else {
              $count_list[] = $type . ': ' . $this_count;
            }
          }
          $count += $this_count;
        }
        if ($count == 0 && $no_show) {
          return NULL;
        }
        if ($count_list) {
          $new_tree[$tid]->counter = '<div class="taxonomy-list-term-count">(' . implode(', ', $count_list) . ')</div>';
        }
        break;
    }
    $new_tree[$tid]->node_count = $count;
    $new_tree[$tid]->term_related = $new_tree[$tid]->term_synonyms = NULL;
    if ($related) {
      if ($relations = taxonomy_get_related($term->tid, 'name')) {
        $names = array();
        foreach ($relations as $related) {
          $names[] = l($related->name, 'taxonomy/vocabulary/' . $term->vid, array(
            'fragment' => $related->tid,
          ));
        }
        $new_tree[$tid]->term_related = '<div class="taxonomy-list-related">' . '<strong>' . t('Related terms') . '</strong>: ' . implode(', ', $names) . '</div>';
      }
    }
    if ($synonyms) {
      if ($syn_list = taxonomy_get_synonyms($term->tid)) {
        $new_tree[$tid]->term_synonyms = '<div class="taxonomy-list-synonyms">' . '<strong>' . t('Synonyms') . '</strong>: ' . implode(', ', $syn_list) . '</div>';
      }
    }
    if ($show_parents && $new_tree[$tid]->parents[0] != 0) {
      $parent_list = array();
      foreach ($new_tree[$tid]->parents as $parent_tid) {
        $parent = $new_tree[$parent_tid];
        $parent_list[] = l($parent->name, 'taxonomy/vocabulary/' . $vocabulary->vid, array(
          'fragment' => $parent->tid,
        ));
      }
      $new_tree[$tid]->parent_list = '<div class="taxonomy-list-parents">[&laquo; ' . implode(' &laquo; ', $parent_list) . ']</div>';
    }
    else {
      $new_tree[$tid]->parent_list = NULL;
    }
  }
  return $new_tree;
}