Monday, October 11, 2010

Truncation of Drupal view node titles

UPDATE

It seems that I very obvious solution for this problem (!), who missed as some commentators, pointed out that is 'Trimming this field to a maximum' - option within the settings for the node title field use.

I'll leave this post in place as I think it still serves as a useful guide to creating template override files for views, but please get governing title is facilitated views node of truncation:

----

Problem:

Have a view (using Drupal's views module) established a list of your site node title, with each node title on the node link outputs.However, one (or more) your node title is really long and it should be truncated.

Truncate Drupal view node title

Solution:

In this example I will use:

Drupal 6.x - 6Views 2.6A view referred to 'test'

The views module makes it super easy for us views fields override and our node title we cut their output to manipulieren.Um to go:

1. Create a template file that overrides the view node title field2.Change the contents of the new template file and implement the reduction

Step 1 - create our new template file:

Navigate to the page "Edit view test" (/ admin/build/view/edit/test) and click the ' topic: information ' link.

Truncate Drupal view node title

A list of possible templates that we can use, to überschreiben.Wir the view you want to override title node field so we go, select one of the template file name specifically provides views ' field node: title (ID: title) '-' views view field - test - Title.tpl. php ' will be the best option, since it heading within the test view override.
Click on ' field node: title (ID: title) ":"

Truncate Drupal view node title

and we see that views also provides us with the code for our new template file:

<>
/ / $ Id: views view field.tpl.php, v 1.1 2008 / 05/16 22: 22: 32 merlinofchaos exp $
/**
* This template is used, a single field in a view to drucken.Es is not
* actually used in default views as is registered as a subject
* Function, the better the performance has. for individual overrides
* Template is perfectly okay.
*
* Variables:
*-$ View: the view object
*-$ Field: the field handler object that can process the input
*-$ Row: the raw SQL result set that can be used
*-$ Output: the processed output are typically used.
*
* At an exit from the $ row fetching this construct should be used:
* $ Data = $ row-> $ field {-> Field_alias}
*
* The above guarantee that you get the right data
regardless of any changes in the aliasing could happen if
* the view is changed.
*/
?>

Copy this code and paste it into a new PHP Datei.Jetzt 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)

Back to the theming information (by clicking on the link ' back to theming information '), and then click again scan template files. views should find and highlight (as it now the most specific) our new views view field - test - title.tpl.php file.

Truncate Drupal view node title

Now we are ready to override the output.

Step 2 - change the contents of our new template file and cut to implement:

Most of the lines of code within the new template file are Kommentare.Der part we are really interested in, is the code that the view node title box outputs:

Basic cut off our node title can be implemented using Drupal's truncate_utf8 function by to change the code in the new file:


$ Output = truncate_utf8($output,25,FALSE,TRUE);
Print $ output;
?>

However, while this will work if our node title easily (no link) are displayed:

Truncate Drupal view node title

try this on node titles that are also links to your node causes links to seemingly disappear:

Truncate Drupal view node title

This is because the truncate_utf8 function counts the length of the entire string uses the field Edition (including html), and not only the length of the output, we on the screen to sehen.Beispielsweise the '..really really long title ' node title's html is actually:

My test page, with A really really really really really really really really really really really really really really long title

What means that using the above performance code results in the only issue that some broken html, along the lines of:

So, to successfully reduce our node title links we must truncate the title we see on the screen and let the surrounding html tags allein.Wir can do this using the following code in our new template file:


$ Title = strip_tags($output); //title (no tags)
$ Title_trunc truncate_utf8($title,25,FALSE,TRUE); = //title cut off (no tags)
$ Output = str_replace($title,$title_trunc,$output); //new-Link
Print $ output;
?>

type the desired result:

Truncate Drupal view node title


View the original article here

No comments:

Post a Comment