I am using cck to create an event form. I would like to use information from the user profile module to populate the cck field. I created a text field in cck that is a select list.

In this case it takes the field profile_cu that contains the following value OIT\nCDL\nOAA\n.

In the allowed values, I use the following php code and it works like a charm.

$query = 'select options from {profile_fields} where `name` = "profile_cu";';
$result = db_query($query);
$field = db_fetch_array($result);
return array_map('trim', explode("\n", $field["options"])); 

The array_map turns it into an array containing the following:

Array
(
    [0] => OIT
    [1] => CDL
    [2] => OAA
)

However, when I go set the default value for the field using the following code I get and error.

$uid = $GLOBALS['user']->uid;
$user = user_load(array('uid' => $uid));
return array(
  0 => array('value' => $user->profile_cu),
);

The error state,"

The PHP code for 'default value' returned Array ( [0] => Array ( [value] => OIT ) ) , which is invalid."

This is odd, because one of the values that the allowed values php code returns is OIT.

My guess is, but I am not sure, is that CCK cannot have both the allowed field populated by php and the default field populated by php. This is only a guess. But, outside of the content, when I run the following code it returns true.

$query = 'select options from {profile_fields} where `name` = "profile_cu";';
$result = db_query($query);
$field = db_fetch_array($result);

//contains the user profile field string value "OIT"
$testuserfield = $user->profile_cu; 

//contains an array with the [0] element containing string "OIT"
$test_dvalue = array_map('trim', explode("\n", $field["options"]); 

if ( $testuserfield == $test_dvalue[0] )
{
print "true";
}
else
{
print "false";
}

When I turn on dpm( get_defined_vars() );, the value contained in the allowed values field is the php and not their returned results.

My question is twofold. Is there a way to set the default value when the allowed values are set via php in cck? Also, if there is not a way to do this in cck, is there an alternative method?

Comments

DruKaz’s picture

I too am having this issue, but with D7.
Have you already found a solution to this?

visabhishek’s picture