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.

Comments

deviantintegral’s picture

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

WorldFallz’s picture

I've looked at this a couple of times and done some googling, but I still haven't found any way to handle this.

casey’s picture

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;
  }
}
WorldFallz’s picture

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?

casey’s picture

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

WorldFallz’s picture

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.

deviantintegral’s picture

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

deviantintegral’s picture

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.

deviantintegral’s picture

Version: 5.x-2.x-dev » 6.x-2.x-dev
Status: Postponed » Needs review
StatusFileSize
new2.71 KB
new1.76 KB

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

freatida’s picture

Drupal 5 version works well for me, thanks.

deviantintegral’s picture

Status: Needs review » Fixed

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

Status: Fixed » Closed (fixed)

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