Is there a way to auto-populate CCK fields during node creation using a users stored values in Content Profile as in the example below using core Profile module?

I used the following in the default value PHP code to fetch a user's name using core Profile module:

global $user;
if ($user->uid) {
profile_load_profile($user);
return array(
   0 => array('value' => $user->profile_name),
  );
}
CommentFileSizeAuthor
#4 job_seeker__content_profile.PNG34.84 KBclashar

Comments

realityloop’s picture

Status: Active » Fixed

I figured this out :)

global $user;
$venue = content_profile_load('profile', $user->uid);
return array(
   0 => array('value' => $venue->field_owneroperator[0]['value']),
);
clashar’s picture

realityloop,

I would like to do the same thing.

I changed your code to correspond to my names, but it doesn't work. Maybe I did some wrong changes, could you please see:

global $user;
$venue = content_profile_load('jobseeker_profile', $user->uid);
return array(
   0 => array('value' => $venue->field_profile_first_name[0]['value']),
);

I changed :
"profile" to "jobseeker_profile", this is the machine name of my content profile.
"field_owneroperator" to "field_profile_first_name", this is the CCK field in "jobseeker_profile".

is it ok?

realityloop’s picture

clashar: I'm guessing you need to use the following:

global $user;
$venue = content_profile_load('jobseeker-profile', $user->uid);
return array(
   0 => array('value' => $venue->field_profile_first_name[0]['value']),
);

Content types are usually hyphenated not underscored..

clashar’s picture

StatusFileSize
new34.84 KB

realityloop,
on my settings for content type it's underscored, please see screenshot.

clashar’s picture

I've tried hyphen, but with no success

realityloop’s picture

Have you checked the 'content profile' option on the Jobseeker Profile content type's edit page?

clashar’s picture

yes of course, it works as content profile and I have fields on user profile form

realityloop’s picture

I'd suggest outputting the field in question using dpm() and the devel module, it may possibly store it's information differently.

clashar’s picture

when I print in dev "Execute PHP Code" script like that:

global $user;
$venue = content_profile_load('jobseeker_profile', $user->uid);
dpm ($venue->field_profile_first_name[0]['value']);

or

global $user;
$venue = content_profile_load('jobseeker_profile', $user->uid);
return ($venue->field_profile_first_name[0]['value']);

it gives me a value with no problem.

Seems that my problem is related to the use of this code in node reference field.

clashar’s picture

I opened a new issue related to node reference #935856: can't get result from "PHP code" in "Default value" section.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sean_a’s picture

I did this and it works. It populated the cck field with the content profile of the logged in user. There was another thread http://drupal.org/node/233378 that helped me figure out how to structure the returned array.

$authorref - made up variable just for this function
profile - content type name

This assumes the current logged in user has a profile, if not the field is left blank.

global $user;
$authorref = content_profile_load('profile', $user->uid);
return array(
   0 => array('nid' => $authorref->nid),
);

For migration purposes, It'd be nice have it set to the profile of the author of the node rather than the logged in user but I'm not sure how to do that.

socialnicheguru’s picture

I have a profile that is a content profile and I allow a person to create several professional experiences.
I have a node reference field_reference_professional. It is unlimited
I return a subset a subset of only the professional experiences that the author has created.

I would like to now set field_reference_professional to all of those by default.

Any idea?

socialnicheguru’s picture

I wrote this to just return one value and am still getting an error:

global $user;
$myprofile_interests = content_profile_load('my_profile_interest', $user->uid);
if ($myprofile_interests !=NULL) {
return array(
   0 => array('nid' => $myprofile_interests->nid),
);
}

I get this:

The default value PHP code returned an incorrect value.
Expected format:
return array(
  0 => array('nid' => value for nid),
  // You'll usually want to stop here. Provide more values
  // if you want your 'default value' to be multi-valued:
  1 => array('nid' => value for nid),
  2 => ...
);
Returned value:
The settings have not been saved because of the errors.