You are here

function taxonomy_list_get_tree in Taxonomy List 7

Same name and namespace in other branches
  1. 5.2 taxonomy_list.module \taxonomy_list_get_tree()
  2. 6.2 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_view in ./taxonomy_list.module
Implementation of hook_block_view().
taxonomy_list_show in ./taxonomy_list.module
Show the category list

File

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

Code

function taxonomy_list_get_tree($vocid, $parent = 0, $op = '', $max_depth = 2147483647) {

  //  drupal_set_message("taxonomy_list_get_tree($vocid, $parent, $op, $max_depth)");
  $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_desc = variable_get('taxonomy_list_show_description', 1);
  $show_parents = variable_get('taxonomy_list_show_parents', FALSE);
  $show_children = variable_get('taxonomy_list_show_children', FALSE) ? '/all' : NULL;
  $ntf_avail = module_exists('node_type_filter');
  $destination = drupal_get_destination();
  $node_types = node_type_get_names();
  $format = variable_get('taxonomy_list_filter', variable_get('filter_default_format', 1));
  $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_term_load($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_list_count_nodes($tid);
    if ($no_show && $count == 0) {
      continue;
    }
    $new_tree[$tid] = $term;
    $path = taxonomy_term_uri($term);
    $term_path = $path['path'] . $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">' . l(taxonomy_image_display($term->tid, NULL, NULL, array(
        'wrapper' => FALSE,
      )), $term_path, $options = array(
        'html' => TRUE,
      )) . '</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-' . $term->tid,
      ),
    )) . '</div>';
    $new_tree[$tid]->desc = $term->description && $show_desc ? '<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' => 'taxonomy/term/' . $term->tid . '/edit',
        '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) . '" />',
        // Max depth not supported in 7.x.
        'href' => 'taxonomy/term/' . $term->tid . '/feed',
        'attributes' => array(
          'title' => t('create feed for "@name"', array(
            '@name' => $term->name,
          )),
        ),
        'html' => TRUE,
      );
    }
    if (count($links) > 0) {
      $new_tree[$tid]->links = theme('links', array(
        'links' => $links,
        'attributes' => array(
          'class' => array(
            'links inline',
          ),
        ),
      ));
    }
    else {
      $new_tree[$tid]->links = '';
    }
    $new_tree[$tid]->counter = '';
    switch ($count_type) {
      case 'none':
        $counter = 0;
        break;
      case 'all':
        $counter = taxonomy_list_count_nodes($term->tid);
        if ($counter == 0 && $no_show) {
          $new_tree[$tid]->counter = '';
        }
        else {
          $new_tree[$tid]->counter = '<div class="taxonomy-list-term-count">' . '<span class="taxonomy-list-term-count-label">' . t('Used by') . '</span>' . ' ' . '<span class="taxonomy-list-term-count-value">' . $counter . '</span>' . '</div>';
        }
        break;
      case 'not_zero':
      case 'by_type':
        $count_list = array();
        $counter = 0;
        foreach (node_type_get_names() as $type => $type_name) {
          $this_count = taxonomy_list_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($node_types[$type] . ': ' . $this_count, $term_path, array(
                'query' => 'type=' . $type,
              ));
            }
            else {
              $count_list[] = $node_types[$type] . ': ' . $this_count;
            }
          }
          $counter += $this_count;
        }

        /* */
        if ($counter == 0 && $no_show) {
          $new_tree[$tid]->counter = NULL;
        }
        if ($count_list) {

          //          $new_tree[$tid]->counter = '<div class="taxonomy-list-term-count">('. implode(', ', $count_list) . ')</div>';
          $new_tree[$tid]->counter = '<div class="taxonomy-list-term-count">' . '<span class="taxonomy-list-term-count-label">' . t('Used by') . '</span>' . ': ' . '<span class="taxonomy-list-term-count-value">' . implode(', ', $count_list) . '</span>' . '</div>';
        }
        break;
    }
    $new_tree[$tid]->node_count = $counter;
    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, drupal_get_path_alias('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;
}