Truncate A Drupal View Node Title
UPDATE
It appears that I missed the very obvious solution to this problem (!) which, as pointed out by a couple of commenters, is to use the ‘Trim this field to a maximum length’ option within the settings for the node title field.
I’ll leave this post in place as I think it still serves as a useful guide on how to create template override files for views, but please be advised that for implementing truncation of a views node title it is easier to:
- navigate to the view's 'Edit view viewname' page (/admin/build/views/edit/viewname) and click the ‘Node: Title' link
- check the ‘Trim this field to a maximum length’ checkbox, and adjust the settings as required
----
Problem:
You've set up a view (using Drupal's views module) which outputs a list of your site's node titles, with each node title linking to its node. However, one (or more) of your node titles is really long and you want to truncate it.

Solution:
In this example I'll be using:
- Drupal 6
- Views 6.x-2.6
- A view called 'test'
The views module makes it super easy for us to override views fields and manipulate their output. In order to truncate our node title we're going to:
- 1. create a template file which overrides the view's node title field
- 2. alter the contents of the new template file and implement the truncation
Step 1 - Create our new template file:
Navigate to the 'Edit view test' page (/admin/build/views/edit/test) and click the 'Theme: Information' link.

Views provides us with a list of possible templates we can use to override the view. We specifically want to override the node title field, so we're going to choose one of the template file names next to 'Field Node: Title (ID: title)' - 'views-view-field--test--title.tpl.php' will be the best option as it will override any title field within the test view.
Now click on 'Field Node: Title (ID: title)':

and we see that views also provides us with the code for our new 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 print $output; ?>Copy this code and paste it into a new PHP file. Now save the PHP file as 'views-view-field--test--title.tpl.php' in the root of your Drupal site's theme folder
(e.g. /sites/all/themes/mythemename/views-view-field--test--title.tpl.php)
Return to the theming information (by clicking the 'Back to theming information' link) and click the 'Rescan template files' button. Views should find and highlight (as it is now the most specific) our new views-view-field--test--title.tpl.php file.

Now we're ready to override the output.
Step 2 - Alter the contents of our new template file and implement the truncation:
The majority of the lines of code within the new template file are comments. The part we are really interested in is the code which outputs the view node title field:
<?php print $output; ?>A basic truncation of our node title can be implemented using Drupal's truncate_utf8 function, by changing the code in the new file to:
<?php
$output = truncate_utf8($output,25,FALSE,TRUE);
print $output;
?>However, whilst this will work fine if our node titles are just plain (no-link) text:

trying this on node titles which are also links to their nodes will cause the links to apparently disappear:

This is because the truncate_utf8 function counts the length of the entire string used to output the field (which includes the html), and not just the length of the output which we see on the screen. For example, the '...really really long title' node title's html is actually:
<a href="/node/1" title="My Test Page With A Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long Title" alt="My Test Page With A Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long Title">My Test Page With A Really Really Really Really Really Really Really Really Really Really Really Really Really Really Long Title</a>which means that using the above truncation code results in the only output being some broken html, along the lines of:
<a href="/node/1" tit ...So, in order to successfully truncate our node title links we need to truncate just the title which we will see on the screen and leave the surrounding html <a></a> tags alone. We can do so using the following code in our new template file:
<?php
$title = strip_tags($output); //title (no tags)
$title_trunc = truncate_utf8($title,25,FALSE,TRUE); //title (no tags) truncated
$output = str_replace($title,$title_trunc,$output); //new link
print $output;
?>giving the desired result:






Hi, I'm Laurence and this is my Drupal blog.
Don't Make Me Think!
Pro Drupal Development
PHP Cookbook
Will You Please Be Quiet, Please?
It's easier to just use:
Trim this field to a maximum length
I'll second that. You can just trim the field. Am I missing something
Indeed, as of Views 2.3 the 'rewrite the output of this field' checkbox opens up several really useful options, including an automatic trim that does not require resorting to theming to do this. It also has some nice code to make sure HTML doesn't get fragmented, which makes it generally better than doing it in theme. In titles this isn't usually a problem, since they can't contain HTML, but it's really quite handy for body fields when you want to do custom teasers.
Hi all, thanks for the comments. I'm not quite sure how I missed that ?! Anyway, I have now updated the post to reflect the easier 'Trim this field to a maximum length' solution.
trimming would be nice when inserting the codes would be that easy
Is there a way to truncate the front of a title in the TRIM THIS FIELD TO A MAX LENGTH?
I have a group of titles like this:
The Old Farmer's Almanac Radio Report for July 31, 2009
The Old Farmer's Almanac Radio Report for July 30, 2009
The Old Farmer's Almanac Radio Report for July 29, 2009
The Old Farmer's Almanac Radio Report for July 28, 2009
And I want the view to look like this:
Radio Report for July 31, 2009
Radio Report for July 30, 2009
Radio Report for July 29, 2009
Radio Report for July 28, 2009
When I put a negative number in the field it trims from the right side
ie: -5 trims them like this
The Old Farmer's Almanac Radio Report for July 31
The Old Farmer's Almanac Radio Report for July 30
The Old Farmer's Almanac Radio Report for July 29
The Old Farmer's Almanac Radio Report for July 28
So I need to do the reverse of the negative number.
Thanks!
Paul I dont know if there is a easier way, but you could use the original solution of this post and instead of using truncate_utf8 use substr
Is it possible to use Node: Menu Title as a field in Views 2? This is generally a shorter title and better suited to the list I'm trying to create, but I don't see the field to pick on the list... Thanks!
I've used the truncate_utf8 method to trim a body field, and it is working, but the length is no longer affected when I use a value greater than '50'. Am I doing something wrong or is there a limit to the truncate length?
Thanks for the clear demonstration of your simple solution. I'm sure this will help a lot of people out. I know it takes a lot of time to write posts like this, so thanks for taking the time.
Is there a way to use regexp to rewrite the out put of a field?
Thanks for sharing this tip.
I'm trying to redesign my website in Drupal and it is harder for me than I expected it will be. Fortunately, I find these useful info and you saved lot of my time! Thanks a lot!
Post new comment