I'm attempting to list out all the fields a user profile object would have into a tableselect. This will be displayed on the configuration screen of a module that I have been building. I have managed to make a tabelselect with checkboxes and dropdowns in it. That part I got down.

The part that I thought would be easy and ended up getting me stuck is getting an array of the fields a profile has. This would include any user made profile fields. Is there a simple way to get an array of the profile fields without needing to relate it to a user? The module would need this list on the front end for anonymous users so comparing it to the user would not work.

I don't need the values of any fields just the names and type of field maybe some settings like max and min. But will not need the value of any set user.

up vote 1 down vote accepted

It sounds like you want field_info_instances():

$fields = field_info_instances('user', 'user');
$rows = array();

foreach ($fields as $field_name => $field) {
  $rows[] = array(
    $field_name,              // eg. 'field_my_field'
    $field['label'],          // eg. 'My Field'
    $field['description'],    // eg. 'A Description of my Field'
    $field['default_value']   // eg. 'default value'
  );
}
  • this works but only shows me an array of the custom fields i added. Is there a way to include the default fields drupal has built into the profile as well? for example the avatar, username, password field, email, One array that has a list of all available fields for a profile. – Patrick W. McMahon Jul 14 '15 at 18:32
  • I would probably just hardcode those because they are pretty standard and usually don't change: username, password, email, status, roles, signature, picture. user_account_form() (api.drupal.org/api/drupal/modules!user!user.module/function/…) essentially does this - it hardcodes the standard form fields, then attaches the generated Field API form elements via field_attach_form (see api.drupal.org/api/drupal/modules!user!user.pages.inc/function/…). – BrianV Jul 14 '15 at 18:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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