I defined a View which displays the list if users. Check the View definition -> in Fields section -> "User: Name" field -> Configure field: User: Name -> More.

There is an option called "Use formatted username. If checked, the username will be formatted by the system. If unchecked, it will be displayed raw."

Whether this option is checked or unchecked, the user name is always truncated. It looks like a bug.

By the way, when I debug the theme_username($variables) function during the view display, there are two values: $variables['name'] and $variables['name_raw'] and the perhaps Views is using the "name" in both cases, instead of "name_raw" for raw display (because "name" is indeed truncated, and "name_raw" is full length).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

dawehner’s picture

Status: Active » Needs review
FileSize
2.2 KB

Well views doesn't truncate something, though maybe some kind of custom theme_username does this.

name_raw is generated by template_preprocess_username, that's something views doesn't have to care about/changes.
'name' is loaded directly from the database so i think views can't do something wrong here. Can you please show an export of your view? Maybe you have configured views to truncate the username.

You have to know that the link-to-user overrides the format-username checkbox.
Here is a small patch, which updates the documentation and makes the code a bit easier to read.

raincloud’s picture

FileSize
11.08 KB

Please find attached my view.

"name_raw is generated by template_preprocess_username, that's something views doesn't have to care about/changes"

I thought that the idea behind the option "Display formatted" was whether to display the user name not truncated (from "name_raw") or truncated (from "name").

In the current state, if I need to display non-truncated username in one view and truncated in another view, it is not possible. (or maybe I don't know yet how)

However, it would be possible, if the code in views/modules/user/views_handler_field_user_name.inc looked like this: adding an additional parameter for theme(username) in the last line - not sure if it's legal to do like this, but I checked and in my view it works ok

  function render_link($data, $values) {
  (....)
    if (!empty($this->options['link_to_user']) || !empty($this->options['overwrite_anonymous'])) {
      if (!empty($this->options['overwrite_anonymous']) && !$account->uid) {
        // This is an anonymous user, and we're overriting the text.
        return check_plain($this->options['anonymous_text']);
      }
      elseif (!empty($this->options['link_to_user'])) {
        $account->name = $this->get_value($values);
        return theme('username', array('account' => $account, 'display_raw' => $this->options['format_username']));
      }
    }
  }

Now it is possible for me to actually make use of this setting from Views UI ("Display formatted") in mytheme_username like this in the first "if" statement:

 function mytheme_username ($variables) {
  if ($variables['display_raw']) {
    $my_name = $variables['name_raw'];
  } else {
    $my_name = $variables['name'];
  }
  if (isset($variables['link_path'])) {
     $output = l($my_name . $variables['extra'], $variables['link_path'], $variables['link_options']);
  }  else {
     $output = '<span' . drupal_attributes($variables['attributes_array']) . '>' . $my_name . $variables['extra'] . '</span>';
  }
  return $output;
}
raincloud’s picture

I tried also something else. Instead of hacking and passing additional argument to theme(username), I set the username to always display the name_raw in my theme(username) function and used a truncation option directly in View definition: Configure field: User: Name -> Style settings -> Trim this field to a maximum length - but then the field disappeared completely from view. Looks like a linked field cannot be truncated, because when unchecked the "Link this field to its user" then the field appears ok (and truncated). But user name must be linked.

tim.plunkett’s picture

Triggering the testbot.

Status: Needs review » Needs work

The last submitted patch, 1445404-username.patch, failed testing.

Morten Najbjerg’s picture

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].

But yes - it seems to be a bug that the username is not displayed as raw when "Link this field to its user" is checked

Dadaisme’s picture

And the bug is still there.

I'll use the quick fix...

thx.

JvE’s picture

There are all kinds of things going on with the user name filed and the options 'link_to_user', 'overwrite_anonymous' and 'format_username'.
This bug appears to be for the case where 'link_to_user' is TRUE and 'format_username' is FALSE.
The expectation is that a link is created to the user profile, showing the raw username. Like <a href="/user/1">$raw_name</a>
Is this correct?

See #1609088: Undefined index: uid in views_handler_field->get_value() (line 375 of views_handler_field.inc) for more information on this functionality and the problems surrounding it.

mmilano’s picture

Here is a workaround to bypass the "link this field to its user" feature that is causing the issue:

- Add a uid field to the view and exclude it from display

- Re-order, and make sure the uid field is above the name field (so it picks up for replacement values)

- Edit the user name field
- - Uncheck "Link this field to its user"
- - Check "Rewrite the output of this field" and set the text value to: [name]
- - Check "Output this field as a link" and set the path to: user/[uid]

arua13l’s picture

Thanks mmilano - worked for me!

Michael_Lessard_micles.biz’s picture

I am adding my confirmation of this bug (usernames are truncated when the option "Link this field to its user" is checked).

It applies to version 6.x-3.0 also.

nb: no matter what I try with Views styles/options and CSS, the usernames are truncated, unless I uncheck that option and apply the workaround named by others above (thanks).

Michael_Lessard_micles.biz’s picture

Issue summary: View changes

edit

prezaeis’s picture

thank u so much worked for me

ezoulou’s picture

Issue summary: View changes

#9 works just fine. thanks.

Tom Ash’s picture

Can we add a patch to fix this problem?

jgullstr’s picture

Version: 7.x-3.1 » 7.x-3.x-dev
Status: Needs work » Needs review
FileSize
1.54 KB

views_handler_field_user_name::render_link() is missing the case when "Link to user" is checked and "Use formatted username" is unchecked. This patch considers it by linking the raw data to user/[uid].

cboyden’s picture

The previous patch fixed the trimming problem, but after applying it, usernames were double-encoded and displaying HTML entities instead of apostrophes. See screenshot:

Screenshot showing HTML entities instead of apostrophes in username generated by Views

The l() function escapes HTML entities by default, but the text being displayed has already been escaped by the render() function in the parent class. Attached is a new patch which also tells the l() function that the text shouldn't be escaped.

hass’s picture

I do not think this patch is correct.

The "Use formatted username" was made to create the Display Name from username. If disabled, only the username should be shown. This is the expected behaviour I think.

I strongly believe this is code that has been upgraded from D6 and it was missed that D7 has added format_username() in template_preprocess_username(). That was not the case in D6 as I know. Therefore if you check the "Use formatted username" it need to run trough theme_username(). The code need to be removed.

// If we want a formatted username, do that.
if (!empty($this->options['format_username'])) {

That the username get's truncated is caused by template_preprocess_username() and I think it is bad in sooo many situations that we need to extend core to allow specifying a param to not truncate or at least truncate at a specified letter. This would be a core addition. I think it would be better to push core first and than add another option in views to allow customizing the truncate position. If core does not change we could add a new theme_views_username function and just clone the core function and add the feature there.

Needs work to cleanup the settings to use theme_username if "Use formatted username" is checked and not if unchecked, but than - leave as. I'm in favor of changing the "Use formatted username" to a selectbox that works like D8 formatter with Plaintext and User name.

I'm willingly to review these patches asap and make them RTBC to get rif of #1239478: How to display a user's username (not real name) in Views once RealName is enabled? in Real name module.

hass’s picture

I'd also like to note #2629286: Use getDisplayName() for user names consistently where I show how many tests are failing and how untested display name feature is. It is D8, but the same exists for sure in D7. The truncating has yielded so many issues... unbelivable.

hass’s picture

jgullstr’s picture

If I'm not mistaken, format_username is needed if you want the formatted username without linking to the user.

hass’s picture

But you should always use the theme username function. Username theming should work with and without linking.

Ronino’s picture

Just rerolled #16 to work with the latest version.