Contact Us

6ta. Avenida 2-48 zona 13, Guatemala, Guatemala.

T +(502) 2458-4464 • Email:

Write us :)

Your budget in US Dollars ($)
Current Website (if you have one)
I want to be part of your team
Can you help us?


We are passionate about Drupal and obsessed with adding significant content to the web. We design, we program, we develop, we comunicate, we teach and blogging.

alex's picture

Truncating fields, from code!

Drupal 7
View Modes

This is a very easy drupal function called "truncate_utf8" it is used to trim text fields or any other string field, a quick example is, if you are working with View Modes and you have to trim the node title and don't find any module to do that, you can do it in an easy way from your custom module with a little function like this.

The structure:
truncate_utf8($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1);
The Parameters
$string: The string to truncate.
$max_length: An upper limit on the returned string length, including trailing ellipsis if $add_ellipsis is TRUE.
$wordsafe: If TRUE, attempt to truncate on a word boundary. Word boundaries are spaces, punctuation, and Unicode characters used as word boundaries in non-Latin languages; see PREG_CLASS_UNICODE_WORD_BOUNDARY for more information. If a word boundary cannot be found that would make the length of the returned string fall within length guidelines (see parameters $max_length and $min_wordsafe_length), word boundaries are ignored.
$add_ellipsis: If TRUE, add t('...') to the end of the truncated string (defaults to FALSE). The string length will still fall within $max_length.
$min_wordsafe_length: If $wordsafe is TRUE, the minimum acceptable length for truncation (before adding an ellipsis, if $add_ellipsis is TRUE). Has no effect if $wordsafe is FALSE. This can be used to prevent having a very short resulting string that will not be understandable. For instance, if you are truncating the string "See for more information" to a word-safe return length of 20, the only available word boundary within 20 characters is after the word "See", which wouldn't leave a very informative string. If you had set $min_wordsafe_length to 10, though, the function would realise that "See" alone is too short, and would then just truncate ignoring word boundaries, giving you "See myverylongurl..." (assuming you had set $add_ellipses to TRUE).
See function documentation at:
//This is a very nice example triming the node title in drupal 7
function MYMODULE_preprocess_node(&$vars) {
if ($vars['type'] == 'article' && $vars['view_mode'] == 'teaser') {
$vars['title'] = truncate_utf8($vars['title'], 50, TRUE, TRUE);
// is an excelent option triming fields and it is used in the module Smart Trim in other fields, as you can see is helpfull in many 
// situations. in this example the first condition is about the content type and the other is the view mode.
// This code will display this as result(example): Dynamically visualize competitive intellectual...