You are here

function tableofcontents_node_view in Table of Contents 7

Implementation of hook_node_view

File

./tableofcontents.module, line 222
This is a filter module to generate a collapsible jquery enabled mediawiki style table of contents based on <h[1-6]> tags. Transforms header tags into named anchors.

Code

function tableofcontents_node_view($node, $view_mode) {
  global $user, $theme_key, $_tableofcontents_block_processing;
  if (!$_tableofcontents_block_processing && isset($node->content['body'])) {
    $processed = FALSE;
    if (variable_get('tableofcontents_nodetype_toc_vtoc_' . $node->type, FALSE)) {

      // ugly test to make sure we don't double the TOC (i.e. if automatic is turned
      // on we would apply the TOC twice when [toc] was used and the filter includes
      // the TOC!)
      if (strpos($node->content['body'][0]['#markup'], 'class="toc"') === FALSE) {
        $node->content['body'][0]['#markup'] = str_replace('[vtoc', '[toc', $node->content['body'][0]['#markup']);
        if ($view_mode == 'teaser' && variable_get('tableofcontents_nodetype_toc_remove_from_teaser_' . $node->type, TRUE)) {

          // remove from teaser or "non-page"
          $node->content['body'][0]['#markup'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $node->content['body'][0]['#markup']);
        }
        else {

          // TODO: if $a3 is true, then we should process the body and save the resulting table in the teaser
          //       (in other words, make sure we get the complete table of content instead of only the teaser part!)
          module_load_include('pages.inc', 'tableofcontents');

          //$format = new stdClass();
          $format = $node->content['body']['#items'][0]['format'];
          $text = _tableofcontents_process($node->content['body'][0]['#markup'], $format);
          if ($node->content['body'][0]['#markup'] != $text) {

            // if tableofcontents_hide_table_<format> is TRUE, then this is wrong...
            if (!variable_get('tableofcontents_hide_table_' . $node->content['body']['#items'][0]['format'], FALSE)) {
              $processed = TRUE;
            }
            $node->content['body'][0]['#markup'] = $text;
          }
        }
      }
    }
    if (!$processed) {

      // is the table of contents block visible?
      $rids = array_keys($user->roles);
      $query = db_select('block', 'b')
        ->fields('b', array(
        'bid',
      ));
      $query
        ->leftJoin('block_role', 'r', 'b.module = r.module AND b.delta = r.delta', array());
      $query
        ->condition('b.delta', 'tableofcontents_block')
        ->condition('b.module', 'tableofcontents_block')
        ->condition('b.theme', array_merge(array(
        $theme_key,
      ), $rids))
        ->condition('b.status', '1')
        ->condition(db_or()
        ->condition('r.rid', $rids, 'IN')
        ->isNull('r.rid'));
      $result = $query
        ->execute()
        ->fetchAll();
      if ($result) {

        // there is a table of contents block, but the node was node parsed...
        // do that now
        module_load_include('pages.inc', 'tableofcontents');
        $format = $node->content['body']['#items'][0]['format'];
        $node->content['body'][0]['#markup'] = _tableofcontents_process($node->content['body'][0]['#markup'], $format, 1);
      }
    }
  }
}