You are here

function _site_map_taxonomy_tree in Site map 7

Same name and namespace in other branches
  1. 5 site_map.module \_site_map_taxonomy_tree()
  2. 6.2 site_map.module \_site_map_taxonomy_tree()
  3. 6 site_map.module \_site_map_taxonomy_tree()

Render the taxonomy tree.

Parameters

string $vid: The results of taxonomy_get_tree() with optional 'count' fields.

string $name: An optional name for the tree. (Default: NULL)

string $description: $description An optional description of the tree. (Default: NULL)

Return value

string A string representing a rendered tree.

1 call to _site_map_taxonomy_tree()
_site_map_taxonomys in ./site_map.module
Render the latest maps for the taxonomy tree.

File

./site_map.module, line 594
site_map.module

Code

function _site_map_taxonomy_tree($vid, $name = NULL, $description = NULL) {
  $output = '';
  $options = array();
  $class = array();
  if ($vid == variable_get('forum_nav_vocabulary', '')) {
    $title = l($name, 'forum');
    $threshold = variable_get('site_map_forum_threshold', -1);
    $forum_link = TRUE;
  }
  else {

    // @codingStandardsIgnoreLine
    $title = $name ? check_plain(t($name)) : '';
    $threshold = variable_get('site_map_term_threshold', 0);
    $forum_link = FALSE;
  }
  $title .= module_exists('commentrss') && variable_get('commentrss_term', FALSE) ? ' ' . theme('site_map_feed_icon', array(
    'url' => "crss/vocab/{$vid}",
    'name' => check_plain($name),
    'type' => 'comment',
  )) : '';
  $last_depth = -1;
  $output .= !empty($description) && variable_get('site_map_show_description', 1) ? '<div class="description">' . filter_xss_admin($description) . "</div>\n" : '';

  // Get the depth.
  $site_map_categories_depth = variable_get('site_map_categories_depth') ? variable_get('site_map_categories_depth') : NULL;
  if ($site_map_categories_depth == 'all') {
    $site_map_categories_depth = NULL;
  }
  $link_empty = variable_get('site_map_link_empty', FALSE);

  // Get the tree in a form that respects access control and translations.
  if (module_exists('i18n_taxonomy')) {

    // Taxonomy Translation.
    $tree = i18n_taxonomy_get_tree($vid, $GLOBALS['language']->language, 0, $site_map_categories_depth);
  }
  elseif (module_exists('entity_translation')) {

    // Entity Translation.
    // Setting the last parameter has performance implications, but adds
    // proper translation support: https://www.drupal.org/node/2418629.
    $tree = taxonomy_get_tree($vid, 0, $site_map_categories_depth, TRUE);
  }
  else {

    // If we don't care about translation, there's no need to lose performance.
    $tree = taxonomy_get_tree($vid, 0, $site_map_categories_depth);
  }
  foreach ($tree as $term) {
    $term->count = site_map_taxonomy_term_count_nodes($term->tid);
    if ($term->count <= $threshold) {
      continue;
    }
    if (module_exists('i18n_taxonomy')) {
      $term->name = i18n_taxonomy_term_name($term, $GLOBALS['language']->language);
    }

    // Adjust the depth of the <ul> based on the change
    // in $term->depth since the $last_depth.
    if ($term->depth > $last_depth) {
      for ($i = 0; $i < $term->depth - $last_depth; $i++) {
        $output .= "\n<ul>";
      }
    }
    elseif ($term->depth == $last_depth) {
      $output .= '</li>';
    }
    elseif ($term->depth < $last_depth) {
      for ($i = 0; $i < $last_depth - $term->depth; $i++) {
        $output .= "</li>\n</ul>\n</li>";
      }
    }

    // Display the $term.
    $output .= "\n<li>";
    $term_item = '';
    if ($forum_link) {
      $term_item .= l($term->name, 'forum/' . $term->tid, array(
        'attributes' => array(
          'title' => $term->description,
        ),
      ));
    }
    elseif ($link_empty || $term->count) {
      $term_item .= l($term->name, 'taxonomy/term/' . $term->tid, array(
        'attributes' => array(
          'title' => $term->description,
        ),
      ));
    }
    else {
      $term_item .= check_plain($term->name);
    }
    if (variable_get('site_map_show_count', 1)) {
      $term_item .= " <span title=\"" . format_plural($term->count, '1 item has this tag', '@count items have this tag') . "\">(" . $term->count . ")</span>";
    }
    if (variable_get('site_map_show_rss_links', 1) != 0) {
      $rss_link = theme('site_map_feed_icon', array(
        'url' => 'taxonomy/term/' . $term->tid . '/feed',
        'name' => $term->name,
      ));
      if (module_exists('commentrss') && variable_get('commentrss_term', FALSE)) {
        $rss_link .= ' ' . theme('site_map_feed_icon', array(
          'url' => "crss/term/{$term->tid}",
          'type' => 'comment',
          'name' => $term->name . ' comments',
        ));
      }
      if (variable_get('site_map_show_rss_links', 1) == 1) {
        $term_item .= ' ' . $rss_link;
      }
      else {
        $class[] = 'site-map-rss-left';
        $term_item = $rss_link . ' ' . $term_item;
      }
    }

    // Add an alter hook for modules to manipulate the taxonomy term output.
    drupal_alter(array(
      'site_map_taxonomy_term',
      'site_map_taxonomy_term_' . $term->tid,
    ), $term_item, $term);
    $output .= $term_item;

    // Reset $last_depth in preparation for the next $term.
    $last_depth = $term->depth;
  }

  // Bring the depth back to where it began, -1.
  if ($last_depth > -1) {
    for ($i = 0; $i < $last_depth + 1; $i++) {
      $output .= "</li>\n</ul>\n";
    }
  }
  _site_map_set_option($options, 'site_map_show_titles', 1, 1, 'show_titles', TRUE);
  $class[] = 'site-map-box-terms';
  $class[] = 'site-map-box-terms-' . $vid;
  $attributes = array(
    'class' => $class,
  );
  $output = theme('site_map_box', array(
    'title' => $title,
    'content' => $output,
    'attributes' => $attributes,
    'options' => $options,
  ));
  return $output;
}