Some of my users' usernames are being trimmed (with ellipses) in blocks and views.

I noticed that the new users block that comes with Drupal core does this, and I also noticed that any view (Views 2 module) that I create where the field "User: Name" is used does it as well. I'm having trouble understanding why this is even happening and it seems to have something to do with the fact that the first name (my users' usernames are two words, always) is rather long.

What makes this happen?
...and how can I stop it from happening?

Comments

dmetzcher’s picture

A different set of keywords turned up the following in search.
http://drupal.org/node/243224

Seems like the link above explains this issue and how to correct it in the theme. Unfortunately, the fix provided did not work. Can anyone else provide a way to increase the number of characters before the username will be truncated?

a6hiji7’s picture

The solution provided in http://drupal.org/node/243224 will work. Basically you have to do the following -

1. In your theme's template.php file write the function themename_username($object). So if you are using the garland theme the function name will be garland_username.
2. Go to http://api.drupal.org/api/function/theme_username and copy the content of the theme_username function as given under the code section
3. Paste it as the content of your function
4. Remove the following lines -
if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';
}
else {
$name = $object->name;
}
and in place of them just write -
$name = $object->name;
5. Clear the Drupal cache
That should do the trick

dmetzcher’s picture

I did this, and instead of removing/replacing the following lines...

if (drupal_strlen($object->name) > 20) {
$name = drupal_substr($object->name, 0, 15) .'...';

...I edited them and did...

if (drupal_strlen($object->name) > 30) {
$name = drupal_substr($object->name, 0, 25) .'...';

It didn't work. Shouldn't it?
I will try removing the lines and replacing them with the line you suggested, but I assumed that it would work by doing it my way and just making the values larger. No?

dmetzcher’s picture

Tried as you said, still doesn't want to work for me.
Here's the code I'm using.

function theme_username($object) {

  if ($object->uid && $object->name) {
    // Shorten the name when it is too long or it will break many tables.
    if (drupal_strlen($object->name) > 40) {
      $name = drupal_substr($object->name, 0, 35) .'...';
    }
    else {
      $name = $object->name;
    }

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => array('title' => t('View user profile.'))));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if (!empty($object->homepage)) {
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
  }

  return $output;
}

I also tried it as you suggested and used this code...

function theme_username($object) {

  if ($object->uid && $object->name) {
    $name = $object->name;

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => array('title' => t('View user profile.'))));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if (!empty($object->homepage)) {
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
  }

  return $output;
}

No luck.

a6hiji7’s picture

Sorry for a very late reply, I did not get any notification about your reply. Hopefully you already found a solution. "function theme_username($object)" - the word "theme" in the function name should be replaced by the name of your theme.

dmetzcher’s picture

Sorry, I was using the correct function name, I simply hadn't changed it in my comments. I am using "function theme_username($object)" and replacing "theme" with the name of my theme. Here's the code and I just tried it again to be certain. I'm getting a blank white page after I upload and refresh.

function litehouse_username($object) {

  if ($object->uid && $object->name) {
    $name = $object->name;

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, array('attributes' => array('title' => t('View user profile.'))));
    }
    else {
      $output = check_plain($name);
    }
  }
  else if ($object->name) {
    // Sometimes modules display content composed by people who are
    // not registered members of the site (e.g. mailing list or news
    // aggregator modules). This clause enables modules to display
    // the true author of the content.
    if (!empty($object->homepage)) {
      $output = l($object->name, $object->homepage, array('attributes' => array('rel' => 'nofollow')));
    }
    else {
      $output = check_plain($object->name);
    }

    $output .= ' ('. t('not verified') .')';
  }
  else {
    $output = check_plain(variable_get('anonymous', t('Anonymous')));
  }

  return $output;
}
zincdesign’s picture

Hi, I am experiencing this issue in Drupal 7 and cannot get the code above to work. Can anyone please help? Thanks!

silvina’s picture

Hi,
In Drupal 7 in order to avoid the username trimming, you should override the template_preprocess_username /http://api.drupal.org/api/drupal/includes--theme.inc/function/template_p...) changing or deleting where says:

(if (drupal_strlen($name) > 20) {
$name = drupal_substr($name, 0, 15) . '...';
}

danny englander’s picture

In Drupal 7, this works for me:

function MYTHEME_preprocess_username(&$vars) {

  // Update the username so it's the full name of the user.
  $account = $vars['account'];

  // Revise the name trimming done in template_preprocess_username.
  $name = $vars['name_raw'] = format_username($account);

  // Trim the altered name as core does, but with a higher character limit.
  if (drupal_strlen($name) > 25) {
    $name = drupal_substr($name, 0, 20) . '...';
  }

  // Assign the altered name to $vars['name'].
  $vars['name'] = check_plain($name);

}

change the numbers as desired and be sure to change MYTHEME to the name of your theme. Put this code in your theme's template.php file.

ace11’s picture

Thanks highrockmedia! That works!

sam152’s picture

If you want to simply show the full username, this can be simplified to:

/**
 * Implements THEME_preprocess_username().
 */
function THEME_preprocess_username(&$vars) {
  $vars['name'] = $vars['name_raw'];
}
tritof’s picture

Thank you Sam152. You saved me some time here.

mulumba’s picture

This worked great!

roborracle’s picture

perfect solution Sam152

maxplus’s picture

Thanks,
works great for me!

Parkes Design’s picture

Great tip thanks Sam152 appreciated!

RKopacz’s picture

For those working in D8, this works in D8 as well. I added the snippet to the YOURTHEME_NAME.theme file for your theme.

Thanks @Sam152!

guyiac’s picture

Yes, this worked for me too - thank you very, very much. But per my comment below, the fact that we even need this entire thread is absurd. Thanks for at least helping to stem the tide of the absurdity.

hansrossel’s picture

maybe add a check_plain for security

$vars['name'] = check_plain($vars['name_raw']);

epringi’s picture

Awesome. Works great. Thanks. :)

guyiac’s picture

I love Drupal, but the fact that we need this whole thread is absurd. Why truncate the data? Why not forward all the data to the theme by default, and let the theme decide how much of it to show in the view? {shakes head}

MatthijsG’s picture

Came here for the same reason: 'why is Views trimming my usernames??' Let the builder decide, not the system.

======
There are 10 people who can count binary
They who can and they who don't

mennonot’s picture

If you don't want to mess around in the code and are just dealing with an issue with user names that are links in views, here's a simpler solution from Morten Najbjerg:

A quick fix for this is to add user uid as a field and hide it.
Then uncheck the "Link this field to its user" and create the link manually using user/[uid].

From: https://www.drupal.org/node/1445404#comment-5808950

carlo_martini’s picture

- Disable "Link to user" in the settings of the Username field

- Use instead a rewrite rule: "Output this field as a custom link" and set it to: /user/

That's it

Rachanabandaplle’s picture

In view's user filed, I selected 'plain text' in formatter instead of User name, and it worked. You can select 'plain text' in formatter and Link to the user and it will work.