I have a question that was asked before and the answer to the question does not work.
I would like to disable user to edit his/her username on the edit page. I would like to display the text as a text but not make it a textfield and give the user the option to update it. How could I do that?

Thanks

Comments

kc’s picture

Well I tried couple suggestions like I said in prev post but having errors. Basically the problem is when you disable diplaying the username in user/edit page then when you try to submit the form it cannot find it and says username cannot be empty or something like that. Somehow the username info should be stored in a hidden form field and not displayed to the user but should be submitted along with other info when submit button is pressed. However I am not a PHP programmer and I cannot figure out how all that can be done.

I will really appreciate if someone can point me in the right direction.

koksal

matanza’s picture

You may need to replace the existing form input with this:

<input type="hidden" maxlength="55" class="form-text" name="edit[name]" id="name" size="30" value="username" />

This is identical to the old input but is hidden and therefore not editable. It does however give the recipient script what it needs to di it's work.

kc’s picture

Thanks for helping,

When I replaced the existing code like in the line below, with your line it hides the username but when you try to save the profile again it overwrites the existing username with the word "username" since the value provided is the string "username" instead of a variable.
I tried $username but did not work. How can I grab the username and stick into the value parameter?

$output  = form_textfield(t("Username"), 'name', $edit['name'], 30, 55, t("Your full name or your preferred username: only letters, numbers and spaces are allowed."));
matanza’s picture

The orignal input entry should have the variable for the username in it. You m ay need to crack open the distribution and check it if yo uhave overwritten the original, otherwise, I used this to get the username variable for apssing to other scripts.

global $user;
$name = $user->name;

Then use $name as the username value.

Cheers

kc’s picture

I really appreciate it. It works now.

KC

nikle’s picture

Here is the file and line with the new code for anyone else who wants to do this.


// user.module line 959 
// changed to disallow username changes, TODO: check if variables are gotten with a POST otherwise this can be circumvented
global $user;
$name = $user->name;
$output  = '<b>HERE</b>input type="hidden" maxlength="55" class="form-text" name="edit[name]" id="name" size="30" value="'.$name.'" /<b>HERE</b>';

// Original code
//   $output  = form_textfield(t("Username"), 'name', $edit['name'], 30, 55, t("If your name is incorrect please contact cusvpcommunications@sauder.ubc.ca."));

I can't submit the < > characters, replace them where you see HERE

fool2’s picture

This code does not allow for the possibility that another user like an admin can edit a users profile. In that case, the code looks like it will pull the admin's name from the $user object instead of the user's name. For this reason I have changed the code a little bit.


 // $group  = form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Your full name or your preferred username: only letters, numbers and spaces are allowed.'), NULL, TRUE);
// disable username edit
$inputarray['uid'] = $uid;
$user1 = user_load($inputarray);
$group = "<input type=\"hidden\" maxlength=55 class=form-text name=\"edit[name]\" id=\"name\" value=\"".$user1->name."\"/>";
// end disable username edit

This code instead pulls the username from a separate user object, $user1. It uses the uid provided to the user_edit function.

This code is around line 1027 in my installation, drupal 4.52 but pretty heavily modified, so try this at your own risk.

syaman’s picture

Hello everyone,
has this functionality (i.e. disabling editing of certain user profile fields) been built into 4.6 or would the above hack still need to be done?

If the latter, is it safe to assume that Fool2's code would work in 4.6 without modifications?

Thank you!

JPCV’s picture

First, my english isn't as good as I wish (I speak spanish) so I would this comment will be usefull and understandable. I modified the user's module to let change the username field only by administrators, and to restrict this posibility to the normal users. I use the Fool2's code and insert a new line.

the changes were in user_edit_form function, line 1039 for 4.6, in this way:

comment line 1040:
$group = form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Your full name or your preferred username: only letters, numbers and spaces are allowed.'), NULL, TRUE);

insert lines:
$group = ""; //Inicializa la cadena por si es un administrador
if(!user_access('administer users')){ //No es administrador, por lo que no puede modificar su username
$inputarray['uid'] = $uid;
$user1 = user_load($inputarray);
$group = "name."\"/>";
}

insert the following line after line number 1044 (in original file)
$group .= form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Your full name: only letters, numbers and spaces are allowed.'), NULL, TRUE);

Bye

Juan Pablo Caballero Villalobos
Alumni Director Engineering Faculty
Pontificia Universidad Javeriana
Bogotá, Colombia

Matthew OMalley’s picture

I found the following to be a slightly simpler way to do this. Replace line 1044 (first line in user_edit_form) with:

  if (user_access('administer users')) {
    $group  = form_textfield(t('Username'), 'name', $edit['name'], 30, 55, t('Your full name or your preferred username: only letters, numbers and spaces are allowed.'), NULL, TRUE);
  }
  else {
    $group  = form_item(t('Username'), $edit['name']);
    $group .= form_hidden('name', $edit['name']);
  }
geme4472’s picture

I realize this thread is pretty much dead (16 months old), but I thought I'd give this a try anyway.

Total newbee. Boss has mandated that all usernames are shown on posts as first/last name from profiles. My initial thought is to add a function that takes the UID, hits the db, concatenates the first/last names, and stuffs the result into $name variable.

I've searched, but haven't found this sort of hack. To make matters worse, my deadline is on this project is 12/30... and did I mention, total newbee?

geme4472’s picture

Thank You! Working on it now...