3

Im using the Advanced Forum module. Many of my users have long usernames which are being cut off (in the author information when they post a comment). Is it possible to lengten the limit before the text is cut off?

I cant see any settings for the Forum or Advanced Forum modules. Below is a theme hook that creates the author information (dont know if this is the right track or not).

/**
 * Theme function to show simple author pane when not using Author Pane.
 */
function theme_advanced_forum_simple_author_pane(&$variables) {
  $context = $variables['context'];
  // Sending the context rather than the account makes it work for anon comments.
  $name = theme('username', array('account' => $context));

  $account = user_load($context->uid);
  $picture = theme('user_picture', array('account' => $account));

  return '<div class="author-pane">' . $name . $picture . '</div>';
}

2 Answers 2

2

It seems Drupal core truncates the username to 25 characters. I have not tried this but this seems to be the solution for Drupal 7 (code added below for reference).

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);

}

Add this code in your theme's template.php file, and change MYTHEME to the machine name of your theme, and change the number 25 to something that fits your theme.

1

The code that truncates the username is in template_preprocess_username(). If you want to get a longer username, you can implement the following code in your theme, or your module.

function mymodule_preprocess_username(&$variables) {
  if (drupal_strlen($variables['name_raw']) > MYMODULE_MAXIMUM_USERNAME_LENGTH) {
    $name = drupal_substr($variables['name_raw'], 0, MYMODULE_MAXIMUM_USERNAME_LENGTH - 5) . '...';
    $variables['name'] = check_plain($name);
  }
}

template_preprocess_username() is executed before mymodule_preprocess_username() (or mytheme_preprocess_username()), which is then executed before theme_username().

As template_preprocess_username() sets $variables['name_raw'] to the raw username, all your module/theme needs to do is to set $variables['name'] truncating $variables['name_raw'] to the length you want. (Replace MYMODULE_MAXIMUM_USERNAME_LENGTH with the desired length.)

  // This is the code executed from template_preprocess_username().

  // 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) > 20) {
    $name = drupal_substr($name, 0, 15) . '...';
  }
  $variables['name'] = check_plain($name);

If you don't want to truncate the username, you can use the following code, in a module, or a theme.

function mymodule_preprocess_username(&$variables) {
  $variables['name'] = check_plain($variables['name_raw']);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.