0

I want to create a basic page that has only one of my custom user account fields listed, and available to edit.

For example, I have a custom user account field: field_companyname

Rather then directing the user to go to www.example.com/?q=user/1/edit, I want the user to go to a different page (i.e www.example.com/node/5). On this page, there should only be a single text input field, and a submit button to save the new data to the user account.

I'm pretty sure I need to implement user_save(), but I just don't know where to go from here.

// load user object
$existingUser = user_load('USERID');

// update some user property
$existingUser->some_property = 'foo';

// save existing user
user_save((object) array('uid' => $existingUser->uid), (array) $existingUser);

Any help or pointers would be much appreciated. Thanks for looking!

EDIT: I was able to load the entire user account edit form onto a node using the following code:

<?php
  module_load_include('pages.inc', 'user', 'user');
  global $user;
  $account = user_load($user->uid);
  $output = drupal_render(drupal_get_form('user_profile_form', $account));
  return $output;
?>

However, I still cannot figure out how to display ONLY my single custom field. Any help is greatly appreciated.

EDIT #2: I used CSS to hide the other fields that I did not want to display. Seems like there should be a better way to do this. But I guess this will work for now, unless anyone else can chime in :-)

Thanks again for looking.

  • if you are using profile2 module use this function to load profile2 fields: profile2_by_uid_load($uid, $type_name) – shrish Dec 20 '15 at 2:24
  • I don't get why you want to do it in a node? Rather do it on a custom form and then you can add form items as you please. You will just be reading the user data and submitting to it but never have to work with the actual account edit form. – J. Reynolds Dec 20 '15 at 2:42
1

Use field_attach_form in your page with the field name and user object. You can use field_attach_form_validate and field_attach_submit to validate and submit the values.

0

Use hide() function hide unnecessary fields. You can hide form field like:

hide($form['field_name']);

Implement hook_form_FORM_ID_alter() to do your magic with a hide() function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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