You are here

function tableofcontents_nodeapi in Table of Contents 6.2

Same name and namespace in other branches
  1. 5.2 tableofcontents.module \tableofcontents_nodeapi()
  2. 6.3 tableofcontents.module \tableofcontents_nodeapi()

Implementation of hook_nodeapi

We need to clear the cache to cover the case where file attachments have changed, but the body hasn't. This might be a little aggressive, in that we clear the cache for any node with attachments, but since this only occurs during editing or creating the load should be pretty minimal.

File

./tableofcontents.module, line 323
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_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'prepare':
      if (isset($node->files)) {

        // Remove the cached version if there attachments on this node
        $cid = $node->format . ':' . md5($node->body);
        cache_clear_all($cid, 'cache_filter');
      }
      break;
    case 'presave':
      if (variable_get("tableofcontents_remove_teaser_{$node->format}", TRUE)) {
        $filters = filter_list_format($node->format);
        if (isset($filters['tableofcontents/0'])) {
          if ($node->teaser && $node->teaser != ($new_teaser = preg_replace('!<\\!-- ?tableofcontents(.*)-->!', '', $node->teaser))) {
            if (strpos($node->body, '<!--break-->') > 0) {

              // We've specfied the split, but the summary is shown in the full
              // view. So, we now have to convert it so that the summary is
              // hidden.
              $node->body = preg_replace('/(.*)<!--break-->/s', '<!--break-->$1', $node->body);
              drupal_set_message(t("Your summary was split from the body as site settings don't allow Table of Contents in summaries."));
            }
            else {

              // This is the case where no specific break or summary was
              // specified, so now it becomes a split teaser.
              $node->body = '<!--break-->' . $node->body;
              drupal_set_message(t("A split summary was automatically created as site settings don't allow Table of Contents in summaries."));
            }

            // Remove toc from teasers.
            $node->teaser = $new_teaser;
          }
        }
      }
      break;
  }
}