Truncate node title in Views
gbrussel - July 14, 2008 - 21:17
I'm not sure whether this is a question that can be easily answered here, or if I should make this a feature request in the Views project...
Working with the latest RC of Views2 with Drupal 6.3, I have a block on the home page listing the 5 latest nodes of a particular type. The node titles (as per the client's request) are somewhat long and run onto multiple lines. Rather than restyling the home page, is there a way to truncate or shorten the node titles in the view? I poked around and didn't find anything in the Views options, but perhaps I missed something.
I'm not an expert in PHP, so if it requires modding of template files or writing new code, bear with me.

See if this helps you
See if this helps you
http://drupal.org/node/238090
Connie
Error
I tried that method, and I'm getting the following error:
Warning: Call-time pass-by-reference has been deprecated; If you would like to pass it by reference, modify the declaration of preg_match_all(). If you would like to enable call-time pass-by-reference, you can set allow_call_time_pass_reference to true in your INI file in /var/www/example.com/html/modules/views/theme/views-view-unformatted.tpl.php on line 22The line in question is:
$field->content, &$matches);I have a really limited understanding of php, but I think there's a different function to call for D6? Any idea what it might be?
diff. D5 to D6 to call function?
Sorry, I haven't delved into D6 yet. Lots of knowledgeable folks around, though. I hope someone can help.
Connie
Help with this issue please!
I have seen this implemented in several drupal site. Long title truncated followed by .... I have created a list of videos using the views grid display. The titles are just too long. I wanted something like this: title ...
Any help on this please
Any help on this please
Solution at last !!!
After weeks of searching I finally found how to truncate item titles in views and add (...) at the end of the truncated title. For some, this is a simple task but for a new non programmer Drupal user, its a great feat. Am putting it down here for other people like me who may be having this 'headache'. Here goes:
There are 2 ways to do this.
(1) Use the phptemplate_views_handle_field_node_title variable to affect ALL views item titles on your site or
(2) Target a specific view by using the phptemplate_views_handle_field_myviewfilename_node_title variable. (within this variable name is myviewfilename which is your view file name. Only this view will be affected.)
Examples
(1) To truncate ALL view item titles on your site to 25 characters followed by ..., post the following code into your template.php. This file is typically found in your theme folder.
function phptemplate_views_handle_field_node_title($fields, $field, $data) {
return l( truncate_utf8($data->node_title, 25, TRUE, TRUE), 'node/'.$data->nid, array('title' => $data->node_title ) );
}
(2) To truncate item titles to 25 characters followed by ... in a specific view called myviewfilename, post the following code into your template.php. This file is typically found in your theme folder.
function phptemplate_views_handle_field_myviewfilename_node_title($fields, $field, $data) {
return l( truncate_utf8($data->node_title, 25, TRUE, TRUE), 'node/'.$data->nid, array('title' => $data->node_title ) );
}
Dont forget to replace myviewfilename with the exact name of your view file in the second example. Also you may vary the number 25 as you see best.
Thats all :)
Thanks!
I will give this a run and see the results. :)
Holy cow you did it!!!!
Holy cow you did it!!!! There are so many threads trying to figure this out and the solution is SO easy! My hat is off to you!
It didn't work for me...
Hi brei9000,
It didn't work for me. Could you please tell me exactly what you did to make it work?
I have a grid gallery view using Views where I would like to shorten the node titles for each thumbnail.
Cheers,
Roger
I just pasted the code into
I just pasted the code into template.php for my theme.
I'm somewhat of a Drupal newb, but it worked just by adding that. Mine was a block in list view - not sure if that makes a difference. I also used it with a custom view.
My code looks like this:
function phptemplate_views_handle_field_frontpage_stories_node_title($fields, $field, $data) {return l( truncate_utf8($data->node_title, 40, TRUE, TRUE), 'node/'.$data->nid, array('title' => $data->node_title ) );
}
frontpage_stories is the name of my view.
Also, here is more documentation http://api.drupal.org/api/function/truncate_utf8/5 (drupal 5).
Still does not work
Hi brei9000
I tried it again (both for all views and for the particular view I would like to truncate titles) but it doesn't work for me.
What version of Drupal are you using? I have D6. Could that be the problem?
Cheers,
Roger
Truncate any field in Views
is very simple in theme views 2 -> Theme: Information-> views-view-fields--(view name).tpl.php, example truncate body comment : in views-view-fields--comments-recent.tpl.php edit after <?php foreach ($fields as $id => $field):
if($id=='comment'){
$field->content=truncate_utf8($field->content,'30', $wordsafe = TRUE, $dots = FALSE).'...';;
}
Views2 theming
Hi jancor,
OK, I had a try based on your description:
1) I checked the possible theming templates via views --> theme --> information and found
views-view-field--title-1.tpl.php
The ID of the field is given as title_1
2) I created the following code that I added to the newly created template file:
<?php
// $Id: views-view-field.tpl.php,v 1.1 2008/05/16 22:22:32 merlinofchaos Exp $
/**
* This template is used to print a single field in a view. It is not
* actually used in default Views, as this is registered as a theme
* function which has better performance. For single overrides, the
* template is perfectly okay.
*
* Variables available:
* - $view: The view object
* - $field: The field handler object that can process the input
* - $row: The raw SQL result that can be used
* - $output: The processed output that will normally be used.
*
* When fetching output from the $row, this construct should be used:
* $data = $row->{$field->field_alias}
*
* The above will guarantee that you'll always get the correct data,
* regardless of any changes in the aliasing that might happen if
* the view is modified.
*/
<?php foreach ($fields as $id => $field):
if($id=='title_1'){
$field->content=truncate_utf8($field->content,'3', $wordsafe = TRUE, $dots = FALSE).'...';;
}
?>
?>
<?php print $output; ?>
3) I saved the file in the theme folder
4) I tested the output but unfortunately the titles are still not cropped. I am not sure if id=='title_1' or field->content is correct. I checked the source code of the page and found the following class for the title
<span class="views-field-title-1">Unfortunately, I don't have firebug installed on this PC (not mine, I am not at home right now) to check further. Perhaps you can see where I made a mistake?
Thanks,
Roger
where was a mistake
I couldn't earlier. I just let know eventually other, that a mistake was in theme-> should be for fields not for field
Another try
OK, I have changed the filename to
views-view-field--photo-album--title-1.tpl.php
(photo-album is the views-name) instead of
views-view-field--title-1.tpl.php
And if I put some test text into the template it is shown right in front of the title I want to truncate. So, at least this seems to be the correct file. However, the title is still not affected by the statement
$field->content=truncate_utf8($field->content,'3', $wordsafe = TRUE, $dots = FALSE).'...';;Please help. What am I doing wrong? I spend probably about 20 hours on this already without any success.... :-(
Thanks,
Roger
A flexible solution for this problem
Hi,
Here's a flexible solution to this type of problem.
For example I have a view named "top_stories" which display as a block showing top 5 stories. In the block display's basic setting, set Row Style to "Node".
I then, in garland theme for example, create a file named "node-view-top_stories.tpl.php". Then in this template file, I can use the $node object. I can start doing anything I want, for example:
<?php
$node->title = truncate_utf8($node->title, 10, false, true); //this will trim the text to 10 characters and with ... at the end
?>
<div id="story-container">
<a href="<?php echo url("node/".$node->nid); ?>"><?php echo $node->title; ?></a>
</div>
Hope this will give you some hints.
BR,
Jim
A better solution
Hi,
A better but more involving solution will be:
1. In the views, for example, my view "top_stories", in the block display Basic Settings section,
set Style to "Unformatted"
set Row Style to "Fields"
2. create file views-view-unformatted--top-stories--default.tpl.php, type in the following code:
<?php if (!empty($title)): ?><h3><?php print $title; ?></h3>
<?php endif; ?>
<?php foreach ($rows as $id => $row): ?>
<div class="<?php print $classes[$id]; ?>">
<?php print $row; ?>
</div>
<?php endforeach; ?>
3. create views-view-field--top-stories--block-1--title.tpl.php, type in the following code:
<?php$row->node_title = truncate_utf8($row->node_title, 10, false, true);
?>
<div id="story-container">
<a href="<?php echo url("node/".$row->node_nid); ?>"><?php echo $row->node_title; ?></a>
</div>
This solution will be better for database load, since it select only certain fields in sql statement, instead of the solution above, load the full node.
The trick to theming views 2 is to understand the "order of the views template hook".
Truncate
Hi Jim,
Thank you very much for this other approach. I am sure it is a very good one. I think for my case it might not be the optimal one since I have a grid view with a thumbnail and about 4 text lines (e.g. title, author, 5 star voting, etc.). I just need to shorten one of the text lines (title of the thumbnail) so that the grid view is not distorted when there is a large title.
I feel that I am close to the solution but something I am still doing wrong.
I now have a template file views-view-field--title-1.tpl.php in my theme folder. title-1 (or title_1 respectively) is the field I would like to shorten. The file contains the following code:
<?php$field->content=truncate_utf8($field->content,20, FALSE, FALSE).'...';
echo 'test';
?>
<?php print $output; ?>
I put echo 'test'; in it to test if the template affects the output of the view. And indeed, 'test' is added just in front of the title I like to truncate. So, the filename seems to be ok and the new template is found by views. Only the single line statement truncate_utf8 doesn't seem to work.
Please if there is anybody who knows what is wrong in above statement please let me know. I don't see where there could be a mistake and I am at the end of my wisdom (and energy to spend on this issue).
Thanks!
Roger
Maybe try this
I think maybe you can try this:
<?php$field->content=truncate_utf8($field->content,20, FALSE, FALSE).'...';
echo 'test';
?>
<?php print $field->content; ?>
There are many other ways to do it too.
If the above does not work
In your file, if you do
<?phpprint_r($row);
?>
You will find a lot of useful information. Find out the corresponding title field, and then truncate it, then print it out. Don't print just the $ouptut, print the field instead.
Truncate
Hi Jim,
Thanks for your patience with me. I have tried what you recommended. The output was that the first grid cell had just '...' as title, the second '......', the third '.........' and so on. It seemed that the title was not found but instead each time a '...' added.
I have used the print_r function. This is what it output:
stdClass Object ( [nid] => 106 [node_data_field_albumimage_field_albumimage_fid] => 196 [node_data_field_albumimage_field_albumimage_list] => 1 [node_data_field_albumimage_field_albumimage_data] => a:0:{} [node_data_field_albumimage_nid] => 106 [node_type] => album [node_title] => Test [users_name] => TestUser [users_uid] => 4 [node_comment_statistics_comment_count] => 2 [node_counter_totalcount] => 131 [votingapi_cache_node_percent_average_value] => 80 [node_created_minute] => 200812291126 )
[node_title] is the one I would like to truncate.
I changed the tpl file to:
<?php
// $Id: views-view-field.tpl.php--title-1.tpl.php
?>
<?php foreach ($fields as $id => $field): ?>
if($id=='title_1'){
$field->content=truncate_utf8($field->content,'20', FALSE, FALSE).'...';
}
<?php endforeach; ?>
<?php print $field->content; ?>
But this time the title line was just empty and no output at all was generated for it....
Could you recommend me what to try based on the output of the print_r statement?
Thanks,
Roger
Try this
<?php$row->node_title=truncate_utf8($row->node_title,'20', FALSE, TRUE);
print $row->node_title;
?>
Attitude is mind's paintbrush, it can color anything.
It works, but
Hi Jim,
It works! :-) Only issue now: before the titles were linked to the corresponding nodes. The links disappeared now. Is there a way how to add them again? If that's possible all is solved :-D
Thanks!
Roger
IT WORKS :-)
Hi Jim,
I worked it out! :-D Here is the full code of my template for others who like to achieve the same thing:
<?php
// $Id: views-view-field.tpl.php--title-1.tpl.php
?>
<?php
$row->node_title=truncate_utf8($row->node_title,'35', FALSE, TRUE);
?>
<?php
$node_to_link = "node/".$row->nid;
$path = drupal_get_path_alias($node_to_link);
?>
<a href="<?php print $path; ?>"><?php print $row->node_title; ?></a>
Thanks a LOT for your help!
Roger
link doubles "node" when the link is displayed from node/abc
Hello Roger
in this example i am using 174 or 175 as node examples, node id will vary depending on your site.
I took at look at your code and tried it out, seemed to work nicely when accessing the link from front page. So for instance the link displayed in the block from the address http://somedomain.com takes you to http://somedomain.com/node/174 nicely. However once you have accessed that node, the links are pre-pended with "node" & $row gives you node/nid, giving you the links like http://somedomain.com/node/node/175 or http://somedomain.com/node/node/174 or what ever the nodes are.
When i get time I will try to fix, however if anyone has a better solution please post here.
thanks
Simon.