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

deviantintegral - December 10, 2007 - 22:09

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

WorldFallz - December 11, 2007 - 01:30

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

casey - December 11, 2007 - 15:25

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

WorldFallz - December 11, 2007 - 14:11

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

casey - December 11, 2007 - 15:58

submit event is fired when a node is stored in the database.

#6

WorldFallz - December 11, 2007 - 16:15

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

deviantintegral - August 18, 2008 - 15:16

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

deviantintegral - October 19, 2008 - 20:22
Status:active» postponed

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

deviantintegral - October 30, 2008 - 16:45
Version:5.x-2.x-dev» 6.x-2.x-dev
Status:postponed» needs review

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

AttachmentSize
191683_remove_teaser_5.x_9.patch 1.76 KB
191683_remove_teaser_6.x_9.patch 2.71 KB

#10

freatida - December 11, 2008 - 14:22

Drupal 5 version works well for me, thanks.

#11

deviantintegral - December 14, 2008 - 03:10
Status:needs review» fixed

Thanks for the testing. I've applied this patch to both versions in commits #158739 and #158740.

--project followup subject--

System Message - December 28, 2008 - 03:13

Automatically closed -- issue fixed for two weeks with no activity.

--project followup subject--

System Message - December 28, 2008 - 03:24

Automatically closed -- issue fixed for two weeks with no activity.

#12

System Message - December 28, 2008 - 03:33
Status:fixed» closed

Automatically closed -- issue fixed for two weeks with no activity.

 
 

Drupal is a registered trademark of Dries Buytaert.