function node_teaser() can "trim out" the entire teaser text (and create invalid html)

snobojohan - August 8, 2007 - 12:13
Project:Drupal
Version:7.x-dev
Component:node.module
Category:bug report
Priority:normal
Assigned:Unassigned
Status:closed
Description

When no delimiter has been specified it tries to split at paragraph boundaries (when it finds a <p> or <br /> or a linebreak etc.). The problem is that it does't check well enough how much actual content will remain. When you have a really long first paragraph this can cause great trouble. When using tinyMCE I get Code like this:

<p>
Lorem ipsum dolor....

In other words there's a linebrek just after the <p> and if the text in the paragraph is longer than the specified trim-lenght node_teaser will cut my teaser text at that linebreak. Leaving me with no text and a broken <p> tag...

For the time beeing I have altered my node.module and added $half_size = $size / 2; and && $length < $half_size

<?php
 
// In some cases, no delimiter has been specified. In this case, we try to
  // split at paragraph boundaries.
 
$breakpoints = array('</p>' => 0, '<br />' => 6, '<br>' => 4, "\n" => 1);

 
// Introducing $half_size
 
$half_size = $size / 2;

  foreach (
$breakpoints as $point => $offset) {
   
$length = strpos($reversed, strrev($point));
   
// Checking that the length of what we cut off is not longer than half the teaser text length asked for
    // Otherwise we let the function go on and split at the end of the last full sentence
   
if ($length !== FALSE && $length < $half_size) {
     
$position = - $length - $offset;
      return (
$position == 0) ? $teaser : substr($teaser, 0, $position);
    }
  }
?>

This works OK for me now, get alot of broken <p> tags that doesn't validate though, an additional function to validate the HTML and close open tags would be nice.

#1

gpk - August 8, 2007 - 14:23

Thanks for posting this issue snobojohan, I was literally just about to do same but you beat me to it!

Previously I reported same problem in a forum topic http://drupal.org/node/160241.

#2

jscheel - September 5, 2007 - 20:05
Category:feature request» bug report

I can confirm this. Fix seems to work fine.

#3

Bevan - October 3, 2007 - 06:23
Status:active» duplicate

Please check if this is the same as the issue described at http://drupal.org/node/180425

#4

gpk - November 26, 2007 - 13:40
Version:5.2» 6.x-dev

It's not a duplicate of that issue but of http://drupal.org/node/155337

#5

manerhabe - November 28, 2007 - 21:38

Thanks so much! This fixed the really strange teasers I was getting.

#6

Bevan - November 29, 2007 - 00:38
Version:6.x-dev» 7.x-dev
Status:duplicate» fixed

This has been improved on in http://drupal.org/node/155337 and http://drupal.org/node/180425.

To the point where the issue described can probably be considered fixed.

Feel free to reset status to active if you plan on contributing this as a patch. I think any patch introducing the approach here would be a feature and need to go into drupal 7.

#7

Anonymous - December 13, 2007 - 00:42
Status:fixed» closed

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

 
 

Drupal is a registered trademark of Dries Buytaert.