prevent toc from appearing in teaser lists
WorldFallz - November 13, 2007 - 02:02
| Project: | Table of Contents |
| Version: | 6.x-2.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Description
Currently, when a page with a TOC appears in a teaser list an empty toc is created at the top of the teaser text (assuming the TOC is at the beginning of the page).
I currently have my users putting a 1 paragraph page abstract at the top of the page, then <!--break-->, then the ToC. Although this works, it's somewhat restrictive and artificial.
A much better solution would be to innately prevent the TOC from rendering on teaser pages. However, it seems that $teaser and $page are unavailable to filters.
This also relates to issue http://drupal.org/node/122064 for adding "Back to top" links as they also should not be rendered on teaser pages.

#1
The site I'm currently working on is using the except module so this doesn't really come up. But, this should probably be dealt with. I don't know if it's possible to change your input filter based on if its a teaser or not, anyone have any thoughts?
--Andrew
#2
I've looked at this a couple of times and done some googling, but I still haven't found any way to handle this.
#3
It's pretty simple though:
function tableofcontents_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {switch($op) {
case 'submit':
$arr = array_keys(filter_list_format($node->format));
if (in_array('tableofcontents/0', $arr)) {
if ($node->teaser) {
// Remove toc from teasers
$node->teaser = preg_replace('!<\!-- ?tableofcontents(.*)-->!', '', $node->teaser);
}
}
break;
}
}
#4
Pardon my ignorance if this is a stupid question-- I'm pretty new to drupal and still trying to learn all the APIs--- there is a submit event prior to every listing of teasers?
#5
submit event is fired when a node is stored in the database.
#6
OIC said the blind man... now i get it. you're method actually removes the toc marker from the $teaser text when it's stored in the db. That sort of goes against drupal's philosophy of not altering the user text, but it's a teaser, which the user normally doesn't enter directly anyway, so I don't have a problem with it.
very cool....thanks for the solution.
@deviantintegral how do you feel about this solution? If you're amenable, I can test it out and roll it into a patch.
#7
This looks like a reasonable solution. Please roll a patch and I'll be glad to test / apply. The regex should probably be defined somewhere so it can be easily changed.
--Andrew
#8
Here is a solution from casey's zip file:
function tableofcontents_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {switch($op) {
case 'submit':
if (variable_get("tableofcontents_noteaser_$format", 1)) {
$arr = filter_list_format($node->format);
if (isset($arr['tableofcontents/0'])) {
if ($node->teaser) {
// Remove toc from teasers
$node->teaser = preg_replace('/<!-- ?tableofcontents(.*)-->/', '', $node->teaser);
}
}
}
break;
}
}
This needs to be written up as a proper patch. I'd like to postpone this until 296171: Change Defaults? lands, as it involves a new setting. I'm going to suggest that this setting not be offered on a per-node basis, as it's almost guaranteed that you don't want the table of contents showing up in teasers.
#9
Here are patches for Drupal 5 and Drupal 6. The Drupal 5 version works as expected. However, the Drupal 6 version has some additional code to handle the D6 split teaser feature. I'm pretty happy with the D5 version, but I'd appreciate some additional testing for D6 before committing.
Thanks!
--Andrew
#10
Drupal 5 version works well for me, thanks.
#11
Thanks for the testing. I've applied this patch to both versions in commits #158739 and #158740.
--project followup subject--
Automatically closed -- issue fixed for two weeks with no activity.
--project followup subject--
Automatically closed -- issue fixed for two weeks with no activity.
#12
Automatically closed -- issue fixed for two weeks with no activity.