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 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"])); 

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.

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?

--Brian

Comments

jaypan’s picture

A) You aren't showing us all the relevant code, which makes it pretty hard to help.
B) This code:

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

Will only return the first option. You need to loop through $result to get all the options - you are only getting the first result from $result.

Contact me to contract me for D7 -> D10/11 migrations.

bnice5000’s picture

My apologies if I have left something out. This is all of the code that I have written.

The field is suppose to only return a single field. The field itself contains several values separated by a "\n" that is why the last line of that code statement is important. return array_map('trim', explode("\n", $field["options"])); turns that field into a usable array that can then be handled by cck. In this case it takes the field profile_cu that contains the following value "OIT\nCDL\nOAA\n" and the array_map turns it into an array containing the following:

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

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";
}

This may be a cck question. Please let me know if have posted to the wrong forum. Thank you for any help in advance.

--Brian

sayantan’s picture

$res = db_query('SELECT * FROM {content_type_agent}');

$df=array(1 => "primary");

while ($o = db_fetch_object($res)) {
$options[$o->field_agentname_value] =$o->field_agentname_value;
}
return array_merge($df,$options);

Save the content type.

Then select default value under default value tab which we have passed using "$df" array variable.

Happy Halloween.
do reply or ask question.

erald’s picture

I have the same kind of problems.
The default values are generated in

$now = date("Y");
$now = $now +3;
$years = array();
for ($i = 1934;$i<$now;++$i) {
$years[] = $i;
}
return($years);

Then I want to have the default value of this year with

$now = intval(date("Y"));
return array(
  0 => array('value' => $now),
);

But it gives me error The PHP code for 'default value' returned Array ( [0] => Array ( [value] => 2011 ) ) , which is invalid.

Some help would be appreciated..