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
I hope this works for you.
views-list-VIEWNAME.tpl.php
I need to do this too. How
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.
Hi! I modified your nicetrim
Hi!
I modified your nicetrim function to make it look nicer (and propably a bit faster too).
I hope you don't mind =)
Here's one I found that
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.
how to call in blocks
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?
newest comment block
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.
thanks for the tip - it
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
be kind to the newbies
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.
Did you ever figure this
Did you ever figure this out? I would like to hear your solution if you have.
http://drupal.org/node/282601
http://drupal.org/node/282601
gbrussel came up with an amazingly simple solution. Just post a function in template.php. Worked for me!