TOC invokes the node_view twice when the TOC block is enabled. I found this out as I had a module of my own which added javascript settings twice in the hook_nodeapi ($op = 'view') function. This messed up the functioning of my module. Also, I believe invoking the node_view function twice reduces performance.
In tableofcontents_block.module the following function is to blame for the invoking of node_view a second time:
<?php
function _tableofcontents_block_view() {
global $_tableofcontents_block_processing;
global $_tableofcontents_block_toc;
// verify access permission
// then whether we are viewing a node
if (!user_access('access content')
|| arg(0) != 'node' || !is_numeric(arg(1))) {
// no block
return;
}
$node = node_load(arg(1));
$_tableofcontents_block_processing = TRUE;
$text = node_view($node, FALSE, TRUE, FALSE);
unset($_tableofcontents_block_processing);
module_load_include('pages.inc', 'tableofcontents');
$_tableofcontents_block_toc = '[toc]';
$text = _tableofcontents_process(0, 'block', $text);
unset($_tableofcontents_block_toc);
if ($text) {
$block['content'] = $text;
$block['subject'] = t('Table of contents');
$block['weight'] = 0;
return $block;
}
// no block
}
?>
May I draw your attention to this part here:
<?php
$text = node_view($node, FALSE, TRUE, FALSE);
?>
Which I have changed to this, so far without ill effect:
<?php
$text = $node->body;
?>
Is there a special reason why node_view() is invoked? $node->body served me just as well. Is it a filtering issue?
If anyone was having trouble with javascript settings and Table Of Contents, this is probably the issue.
Comments
Comment #1
AlexisWilke commentedTommy,
Yes. I call node_view() to get the node as it will be printed. Then the node system will call it again to create the actual node output.
You'd need to test a flag in your code to know whether you already done the work or not.
Your solution may work for you, but it would not be as good for people like me who use [node], [view], ... filters that can include additional headers that we want to be part of the table of contents. Also, even a header could include a tag as in:
1. Blah title (Last changed on [node:updated])
I guess we could add a flag in the settings to let the user choose whether the filters should be applied or not. If not, then we could use the simple xss filter against the body. (using the body bare is okay on a site you control 100%, if someone you don't trust has right to create node content, then they could include an XSS attack.)
Thank you.
Alexis Wilke
Comment #2
tommy kaneko commentedI see. I assumed it was a way to make sure that filters were applied before the TOC was generated. I have looked though the drupal API, and I can see that the code you already have is the simplest one (which tends to mean the best).
I have put in the checks in my module as you suggested. I hope this post will help someone diagnose Javascript issues with TOC.
Great module, btw.
Comment #3
AlexisWilke commentedOkay, good. 8-)
I'll mark this as a feature request for a flag to choose between full filtering or quick XSS filter of the body for the block.
Thank you for the last complete report.
Alexis Wilke