Hi,

I have a user form that when logged in I would like to automatically pull the users profile fields as default values for name, company, phone number etc.. I am using the bio module which I have renamed to "account". I tried to create a custom module as well insert the code directly in the default value of the cck field. However, the most I can get is "1". Any suggestions?

$qname = db_query("SELECT (field_account_fname_value) FROM (content_type_account) INNER JOIN node ON (content_type_account.nid = node.nid) WHERE (node.uid = $user->uid)");

Comments

mrtoner’s picture

The query is unnecessary. All of the current user's profile fields are quickly available by loading the user object:

<?php
$uid = $GLOBALS['user']->uid;
$user = user_load(array('uid' => $uid));

print $user->profile_firstname;
?>

Just replace "profile_firstname" with "profile_" and the name of your profile field.

Anonymous’s picture

Okay.. for anyone who wants to populate a CCK field from another CCK field that a user has previously completed (such as bio field) you can create a custom module and add the following for each field you would like to include a default value for. The reason why I needed this function was to allow customers, when logged in, to have their profile fields automatically populated when submitting a quote inquiry. I didn't want to use webform because I needed to be able to setup custom views to sort by location, product etc. for the sales team.


function mycustommodule_form_alter($form_id, &$form) {
  if ($form_id == 'quote_node_form') {
    global $user;
    $bio_node = node_load(array('type' => 'bio', 'uid' => $user->uid));
    $form['field_quote_name'] = array (
		'#type' => textfield,
		'#required' => TRUE,
		'#title' => 'Full Name',
		'#size' => 25,
		'#default_value' => $bio_node->field_bio_fname[0]['value']);
  }
}


internets’s picture

Could you please share the steps you took in creating / setting up the module and having it called from your form.

I'm trying to take cck user profile data (created with the content_profile module http://drupal.org/project/content_profile)

I tried simply making a module with the your code and enabling it but I'm not getting any fields to populate.

I made a .module file

profile_data_to_cck.module

/**
* @file
* populating cck fields with user data created with content_profile module.
* 
* http://drupal.org/node/300600
* @see http://drupal.org/node/300600
*/

function profile_data_to_cck_form_alter($form_id, &$form) {
  if ($form_id == 'node-form') {
    global $user;
    $contact_detail_node = node_load(array('type' => 'contact_details', 'uid' => $user->uid));
    $form['title'] = array (
'#type' => textfield,
'#required' => TRUE,
'#title' => 'Services',
'#size' => 60,
'#default_value' => $contact_detail_node->title[0]['value']);
  }
}

I created the .info file and enable the module but see no field getting populated.

I've also tried changing the

'#default_value' => $contact_detail_node->title[0]['value']);

to, using the field's name

'#default_value' => $contact_detail_node->[title][0]['value']);

and tried the input id 'edit-title'
nothing has worked yet.

Any suggestions appreciated.
Thanks