You are here

function theme_tableofcontents_toc in Table of Contents 7.2

Same name and namespace in other branches
  1. 5.2 tableofcontents.module \theme_tableofcontents_toc()
  2. 6.3 tableofcontents.pages.inc \theme_tableofcontents_toc()
  3. 6.2 tableofcontents.module \theme_tableofcontents_toc()
  4. 7 tableofcontents.pages.inc \theme_tableofcontents_toc()

Theme the output of a table of contents.

Parameters

$toc A TOC object with the options, table of contents, headers, and files.:

Return value

Rendered HTML to be displayed.

2 theme calls to theme_tableofcontents_toc()
tableofcontents_field_attach_view_alter in ./tableofcontents.module
Implementation of hook_field_attach_view_alter(&$output, $context)
_tableofcontents_process_text in ./tableofcontents.module
Developer function to apply TOC to any text $body has two assumptions for this function to work 1. It must have [toc] located somewhere in the text 2. It has already been processed by an input filter with toc enabled

File

./tableofcontents.themes.inc, line 14
Table of Contents - Versatile system for generating Tables of Contents for fields - themes.

Code

function theme_tableofcontents_toc($variables) {
  $toc = $variables['toc'];

  // TODO: we should only define the id="toc" for the first TOC
  if ($toc['back_to_top']['anchor']) {
    $id = ' id="' . check_plain($toc['back_to_top']['anchor']) . '"';
  }
  else {
    $id = '';
  }
  $output = "<div{$id} class='toc'>\n";
  if ($toc['box']['title'] != '<none>') {

    // TODO: at this time, the hideshow works with all TOCs at once
    $hs = $toc['box']['hide_show'] ? '<span class="toc-toggle-message">&nbsp;</span>' : '';
    $output .= "<div class='toc-title'>" . t($toc['box']['title']) . $hs . "</div>\n";
  }
  $list = $toc['numbering']['method'] == 4 ? 'ol' : 'ul';
  $output .= "<div class='toc-list'>\n<{$list}>\n";
  $has_content = FALSE;

  // Process nested lists.
  $level = $toc['box']['minlevel'];
  if (!isset($toc['header']['h'])) {
    $toc['header']['h'] = array();
  }
  foreach ($toc['header']['h'] as $index => $h) {
    $cur_level = $h->level;
    if ($cur_level >= $toc['box']['minlevel'] && $cur_level <= $toc['box']['maxlevel']) {
      $has_content = TRUE;

      // Be sure to deal with skipping between non-adjacent h levels.
      if ($cur_level > $level) {
        do {
          $output .= "\n<" . $list . ">\n";
          ++$level;
        } while ($cur_level > $level);
      }
      elseif ($cur_level < $level) {
        do {
          $output .= "</li>\n</" . $list . ">\n";
          --$level;
        } while ($cur_level < $level);
        $output .= "</li>\n";
      }
      elseif ($index != 0) {

        // we know that $cur_level == $level here
        // Close list items at the same level (except the very first time)
        $output .= "</li>\n";
      }
      if ($h->number) {
        $number = '<span class="toc-number">' . $h->number . '</span>';
      }
      else {
        $number = '';
      }
      $list_class = 'toc-level-' . ($level - $toc['box']['minlevel'] + 1);
      $title = strip_tags($h->title, $toc['header']['allowed']);

      // insert the li element
      $output .= "\t<li class=\"{$list_class}\">";
      $output .= "<a href=\"#" . $h->ident . "\">" . $number . $title . "</a>";
    }
  }

  // Did we recurse back out? If not, close open lists.
  for (; $level > $toc['box']['minlevel']; --$level) {
    $output .= "</li>\n</" . $list . ">\n";
  }
  $output .= "</li>\n";
  if (!$has_content) {

    // this happens when all the header levels are either too small or too large
    // and if there is no attachment either
    return '';
  }
  $output .= "</" . $list . ">\n</div>\n</div>";
  return theme('tableofcontents_toc_text', array(
    'toc' => $toc,
    'output' => $output,
  ));
}