Hi everyone

On my test-site it looks like Drupal only displays the first 15 characters of a username.

I have one test user with >15 characters in his name (humpty dumpty was pushed).

In a view (table-style) his name displays shortened as humpty dumpty w...
Even in full node written by him, it appears as humpty dumpty w...

I suspect I have to go and change something in an actual file somewhere? I have around on this site looked for a solution but can't see anything obvious - sorry if this has been asked before but there's a chocolate donut for the first person to find me an answer...

Thanks

Paul

Comments

sepeck’s picture

It may be an artifact of your theme. Try a different theme and see if you have the same issue. If not, then you've at least narrowed it down.

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide -|- Black Mountain

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

doctorpaul’s picture

Thanks Steven

I will try that in a bit - (I'm struggling with views now). I am using Garland - the basic theme if that helps anyone else answer the question in the meantime

Paul

gpk’s picture

If the username is more than 20 characters it is truncated to 15. This is the default behaviour of http://api.drupal.org/api/function/theme_username/5. You can override it by implementing mytheme_username() in your theme's template.php (suggest you work on a copy of Garland, installed in sites/all/themes and with a new name, rather than the original).

gpk
----
www.alexoria.co.uk

doctorpaul’s picture

Thanks for your info gpk. Appreciate it.

I don't fully understand the answer because I don't know how to

"override it by implementing mytheme_username() in your theme's template.php"

- I have much to learn.

But I followed your link and straight away noticed this line of comment in the code:

   // Shorten the name when it is too long or it will break many tables.

I wondered what the reasoning was and now this may confirm that it's not just Drupal being niggly. Does anyone know if this is a real problem? Perhaps I'm just better off leaving things as they are. After all: 15 letters will be enough for most people...

gpk’s picture

It's probably talking about the user admin pages, but since you can leave Garland as the theme for the admin section you can safely increase this limit in your own theme or own version of Garland.

>I don't fully understand the answer
1. Copy the garland folder from the main themes directory in your drupal installation to sites/all/themes, and call the new folder garland_custom.

2. At the bottom of the file template.php in the garland_custom folder, add the following (without the opening and closing PHP tags):

/**
 * Format a username with a higher limit for the length displayed than the
 * default in theme_username().
 *
 * This implementation is 99% based on theme_username().
 *
 * @param $object
 *   The user object to format, usually returned from user_load().
 * @return
 *   A string containing an HTML link to the user's page if the passed object
 *   suggests that this is a site user. Otherwise, only the username is returned.
 */
function phptemplate_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) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, 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 ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

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

  return $output;
}

Then change the values 15 and 20 to whatever you want. Note you could also call this function garland_custom_username(), I won't dwell on that now, won't make any practical different to you ...

3. Enable the new theme from your admin interface and optionally choose a theme to use for the admin pages by visiting admin/settings/admin.

BTW there is meant to be a space before the lines that begin with *. Not important but makes the code neater (that's the standard Drupal comment style...)

gpk
----
www.alexoria.co.uk

doctorpaul’s picture

Thanks for the clear step-by-step stuff gpk. Much appreciated from this new person.

alex.ihlo’s picture

I post this into my templates.php file for my currently enabled theme, and it returns this error:

Fatal error: Cannot redeclare theme_username() (previously declared in /home5/greypage/public_html/includes/theme.inc:1593) in /home5/greypage/public_html/sites/nm.greypages.us/themes/aboutpeople/template.php on line 220

gpk’s picture

Indeed, when copying theme_username() into template.php it needs to be renamed to themename_username() [or possibly phptemplate_username(), but this is not recommended for D6+].

dmetzcher’s picture

I attempted to do this in my theme and only got a blank white screen after uploading the modified template.php file. I'm running Drupal 6, and I believe this was posted for Drupal 5, but it should still work if the API page is correct. Any thoughts?

gpk’s picture

Yes should still work though for D6 the preferred name for the function would probably be themename_username() where themename is the name of your theme.

If wanting to modify an existing core or contrib theme in this way then that could be done by declaring a subtheme (http://drupal.org/node/225125) rather than copying the original and modifying the copy.

dmetzcher’s picture

I've added the following to the bottom of my template.php file. I'm using the themename_username() format. The name of my theme is litehouse. I'm getting a blank page when I load anything that uses the theme.

Have you tested this on a Drupal 6 site or what it for Drupal 5? I think Drupal 6 doesn't like it. :(

/**
* Format a username with a higher limit for the length displayed than the
* default in theme_username().
*
* This implementation is 99% based on theme_username().
*
* @param $object
*   The user object to format, usually returned from user_load().
* @return
*   A string containing an HTML link to the user's page if the passed object
*   suggests that this is a site user. Otherwise, only the username is returned.
*/
function litehouse_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) > 20) {
      $name = drupal_substr($object->name, 0, 15) .'...';
    }
    else {
      $name = $object->name;
    }

    if (user_access('access user profiles')) {
      $output = l($name, 'user/'. $object->uid, 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 ($object->homepage) {
      $output = l($object->name, $object->homepage);
    }
    else {
      $output = check_plain($object->name);
    }

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

  return $output;
}
pensatu’s picture

Thanks for this step..

gregoiresan’s picture

Hi,

I am having the same need. My site is a Market Place and my sellers always wanna add extension such as : Corp., Company, Team, Friends, ...etc.

When they register, there is no limit, so they wonder why they name is always trunk ?

I noticed that the full view of the user shows the full name. However, I can't show the full name with views ?? Is it a View issue or theme issue ? (same problem with Garland or Fusion).

I don't think this will affect databases tables as they are already recording full names... so, why making this trimmed with PHP for all other views ?

In my point of view, this is extra code, useless as long as users want to be named as they suggested. Also, for SEOs, this makes it weak in a user point of view : "How can I be recognized by SEOs if my name is trunk ?"

Useless also because we can choose to restrict size limit in the Access Rules section.

Really, I don't understand this limit...

zincdesign’s picture

Hi,
I am experiencing the same issue in Drupal 7. Can anyone help me please? The code above doesn't work.

The username appears fine in their account page but not in a View. I would like the full user name to be displayed.

Thanks in advance!

timothykc’s picture

I'm also running into the same issue, and can't quite figure out a fix.

timothykc’s picture

My solution was to use the realname module.

Configure it with whatever replacement pattern you want.

In VIEWS you'll now be able to add the "real name" field, with an untrucated user name!

Roger34’s picture

I had the same issue. RealName module resolved it. However, I am not sure what is the safe maximum length without breaking the table. I use D6

elgrincho’s picture

You can try to put this into your template.php. Works for me in Drupal 7

function yourthemename_username($object) {

    
  if ($object['uid'] && $object['name']) {
    // set the variable
    $name = $object['account']->name;

    if (drupal_strlen($name) > 100) {
    // Shorten the name when it is too long or it will break many tables.
      $name = drupal_substr($name, 0, 100) .'...'; 
    }    

    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;
}
robcarr’s picture

Bit late to this very old thread, but probably best do the override in the theme layer with _preprocess_username() (lines 19 & 20 are where you can alter the values):

function yourthemename_preprocess_username(&$variables) {
$account = $variables['account'];
$variables['extra'] = '';
if (empty($account->uid)) {
$variables['uid'] = 0;
if (theme_get_setting('toggle_comment_user_verification')) {
$variables['extra'] = ' (' . t('not verified') . ')';
}
}
else {
$variables['uid'] = (int) $account->uid;
}

// Set the name to a formatted name that is safe for printing and
// that won't break tables by being too long. Keep an unshortened,
// unsanitized version, in case other preprocess functions want to implement
// their own shortening logic or add markup. If they do so, they must ensure
// that $variables['name'] is safe for printing.
$name = $variables['name_raw'] = format_username($account);
if (drupal_strlen($name) > 35) {
$name = drupal_substr($name, 0, 30) . '...';
}
$variables['name'] = check_plain($name);
$variables['profile_access'] = user_access('access user profiles');
$variables['link_attributes'] = array();

// Populate link path and attributes if appropriate.
if ($variables['uid'] && $variables['profile_access']) {

// We are linking to a local user.
$variables['link_attributes'] = array(
'title' => t('View user profile.'),
);
$variables['link_path'] = 'user/' . $variables['uid'];
}
elseif (!empty($account->homepage)) {

// Like the 'class' attribute, the 'rel' attribute can hold a
// space-separated set of values, so initialize it as an array to make it
// easier for other preprocess functions to append to it.
$variables['link_attributes'] = array(
'rel' => array(
'nofollow',
),
);
$variables['link_path'] = $account->homepage;
$variables['homepage'] = $account->homepage;
}

// We do not want the l() function to check_plain() a second time.
$variables['link_options']['html'] = TRUE;

// Set a default class.
$variables['attributes_array'] = array(
'class' => array(
'username',
),
);
}