Hey all!

I've been at this for the best part of the day, and it's driving me crazy! Can't seem to find anything useful by searching drupal.org, and I'm at the end of my rope... Can anybody help?

What I want to do:
I have a list view displaying title links to nodes of a certain content type, and Node-reference links to referenced content. However, the titles of these links are often too long for the layout, and i want to trim them as follows:

The list view default

  • This title link is extremely long and is the title of the node
  • This title link is also very long, and is the Node-reference link
  • This one is short

What I want

  • This title link is ext...
  • This title link is als...
  • This one is short

I am thinking that this should definitely be possible, and I'm surprised that it's been so difficult to find any info about it (or I suck at searching).

What I have so far:
...is a not-so-elegant attempt, which doesn't work as I want it. I've placed this code in my template.php file. It's a function I found over at www.php.net, and it trims a string of words to a defined length.


function nicetrim ($s) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters
// and adds "..."
// It counts HTML char such as á as 1 char.
//

  $MAX_LENGTH = 25;
  $str_to_count = html_entity_decode($s);
  if (strlen($str_to_count) <= $MAX_LENGTH) {
    return $s;
  }

  $s2 = substr($str_to_count, 0, $MAX_LENGTH - 3);
  $s2 .= "...";
  return htmlentities($s2);
}

In my views-list-VIEWNAME.tpl.php i have done the following:

  $text = $title;
  strip_tags(html_entity_decode($text), "<a>");
  print '<a href="' . '' . '">' . nicetrim(strip_tags($text)) . '</a>';
  

The "strip_tags" removes the link tags which follow the $title, or I would get "<a href="mysite.com/t..." when it trims. So now I get the title trimmed, but the link links to whatever page is current. I tried to remedy that by adding the link tags on either side of the trimmed title, and then $node->url, but that didn't do the trick - it didn't register at all...

All in all, I feel like when I cure one problem, two new ones arise, and that it culminates in a really clumsy "solution".

If anyone can offer a hand here, I would be very grateful!

Best regards,
joeboris

Comments

color2life’s picture

views-list-VIEWNAME.tpl.php

   <?php

  $text = $title;
  strip_tags(html_entity_decode($text), "<a>");

  
  
  preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+".
                    "(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/",
                    $field->content, &$matches);
        
    $matches = $matches[1];
    $list = array();

    foreach($matches as $var)
    {    
         print '<a href="' . $var . '">' . nicetrim(strip_tags($text)) . '</a>';
    }

 
?>
JonMB’s picture

I need to do this too. How do I use your solution? I tried inserting the code into my template.php, and then creating the views-list-names.tpl.php (my Views List is named News) but it doesn't seem to do anything.

Coornail’s picture

Hi!

I modified your nicetrim function to make it look nicer (and propably a bit faster too).

<?php
function nicetrim ($s) {
// limit the length of the given string to $MAX_LENGTH char
// If it is more, it keeps the first $MAX_LENGTH-3 characters
// and adds "..."
// It counts HTML char such as &aacute; as 1 char.
//
  define('MAX_LENGTH', 25);

  $str_to_count = html_entity_decode($s);
  if (strlen($str_to_count) > MAX_LENGTH) {
    $s = substr($str_to_count, 0, MAX_LENGTH - 3) . '...';
  }

  return htmlentities($s);

}
?>

I hope you don't mind =)

awolfey’s picture

Here's one I found that works really well and is flexible for any variety of uses. You can find some more options and instructions on the developer's site. I put it in template.php and call it in a few different blocks.

// Original PHP code by Chirp Internet: www.chirp.com.au ----------breaks strings at words, used by commentrand, newest comments
// Please acknowledge use of this code by including this header. 
function myTruncate($string, $limit, $break=".", $pad="...") { 
// return with no change if string is shorter than $limit  
if(strlen($string) <= $limit) return $string; 
// is $break present between $limit and the end of the string?  
if(false !== ($breakpoint = strpos($string, $break, $limit))) { 
  if($breakpoint < strlen($string) - 1) { 
    $string = substr($string, 0, $breakpoint) . $pad; 
  } 
  } 
return $string; }
holydiver-1’s picture

How do you call it in a few different blocks? I'm sorry if this is completely moronic, but I don't really know much programming. Do you just write the function in a block? What arguments do you put into it?

awolfey’s picture

Here's the php in a block that shows a truncated most recent comment as a link to the full comment, and puts it in quotes. Please note that I am not a programmer so this may not be the best or most secure way of doing this. You can use the same function but supply it with different variables for the string and length you want. the " " tells it to truncate after a complete word. Again, see the site mentioned above for the details.


<?php
$listlength="1";
$charlength="35";
unset ($output);
$result1 = pager_query("SELECT c.comment, c.timestamp, c.status, c.nid, c.cid FROM {comments} c WHERE c.status = 0 ORDER BY c.timestamp DESC", $listlength);
while ($comment = db_fetch_object($result1)) {
$anchor .= 'comment-'. $comment->cid;
$output .= '<div class="last-comment">';
$newcomment = myTruncate($comment->comment, $charlength, " ");
$output .= "&#8220;". l(strip_tags($newcomment), 'node/'. $comment->nid, array(), NULL, $anchor). "&#8221;";
}
$output .= '</div>';
print $output;
?>

omnyx’s picture

thanks for the tip - it seems to work though I got rid of the double apostrophes in the last $output. and put li's instead of the divs

ibexy’s picture

alright I figured out how to add one of these code blocks above to my template.php. But how do I make my views recognise and use this solution on my titles. I got long titles distorting my views (grid display). Someone please give a step by step method of achive a truncated title on views please.

brei9000’s picture

Did you ever figure this out? I would like to hear your solution if you have.

brei9000’s picture

http://drupal.org/node/282601

gbrussel came up with an amazingly simple solution. Just post a function in template.php. Worked for me!