function tableofcontents_nodeapi in Table of Contents 6.3
Same name and namespace in other branches
- 5.2 tableofcontents.module \tableofcontents_nodeapi()
- 6.2 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. It also only happens if the node has file attachments.
File
- ./
tableofcontents.module, line 176 - 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) {
global $user, $theme_key, $_tableofcontents_block_processing;
switch ($op) {
case 'prepare':
if (isset($node->files)) {
// Remove the cached version if there are 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)) {
module_load_include('admin.inc', 'tableofcontents');
_tableofcontents_hide_in_teaser($node);
}
break;
case 'insert':
case 'update':
_tableofcontents_save($node);
break;
case 'load':
_tableofcontents_load($node);
break;
case 'view':
// $a3 represents the $teaser flag
// $a4 represents the $page flag
if (!$_tableofcontents_block_processing) {
$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']['#value'], 'class="toc"') === FALSE) {
$node->content['body']['#value'] = str_replace('[vtoc', '[toc', $node->content['body']['#value']);
if (!$a4 || $a3 && variable_get('tableofcontents_nodetype_toc_remove_from_teaser_' . $node->type, TRUE)) {
// remove from teaser or "non-page"
$node->content['body']['#value'] = preg_replace(TABLEOFCONTENTS_REMOVE_PATTERN, '', $node->content['body']['#value']);
}
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');
$text = _tableofcontents_process(0, $node->format, $node->content['body']['#value']);
if ($node->content['body']['#value'] != $text) {
// if tableofcontents_hide_table_<format> is TRUE, then this is wrong...
if (!variable_get('tableofcontents_hide_table_' . $node->format, FALSE)) {
$processed = TRUE;
}
$node->content['body']['#value'] = $text;
}
}
}
}
if (!$processed) {
// is the table of contents block visible?
$rids = array_keys($user->roles);
$sql = "SELECT b.bid FROM {blocks} b" . " LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta" . " WHERE b.delta = '0' AND b.module = 'tableofcontents_block' AND b.theme = '%s'" . " AND b.status = 1 AND (r.rid IN (" . db_placeholders($rids) . ") OR r.rid IS NULL)";
$sql = db_rewrite_sql($sql, 'b', 'bid');
$result = db_query($sql, array_merge(array(
$theme_key,
), $rids));
if (db_fetch_array($result)) {
// there is a table of contents block, but the node was node parsed...
// do that now
module_load_include('pages.inc', 'tableofcontents');
$node->content['body']['#value'] = _tableofcontents_process(1, $node->format, $node->content['body']['#value']);
}
}
}
break;
}
}