Summary
Admin cannot set custom PHP code to set the default value of a CCK User Reference field to the currently logged in user, when the currently logged in user (Admin) is not one of the "User roles that can be referenced".
Details
- Logged in as admin, configure a CCK User Reference field.
- Go to URL http://dev.server.lan/admin/content/types/CCKtype/fields/CCKUserRefField
where CCKtype is the name of the CCK type and CCKUserRefField the name of the CCK field - Go to admin setting: Widget > Default value > Php code
- Enter custom code in order to define a non-standard default value for the CCK User Reference (without php-tags as per instructions):
global $user; return array(0 => array('uid' => $user->uid)); - Go to admin setting (same page): "User roles that can be referenced"
- Select some role, EXCEPT authenticated user (although, I'm not sure the latter would make any difference)
Problem
When admin is submitting the form, validation fails with following error messages:
- CCKfieldname : Invalid user.
This message is correct, but Admin was not meant to create nodes/CCK field values; this error message seems irrelevant here. - The default value php code created Array ( [0] => Array ( [uid] => 1 ) ) which is invalid.
The PHP code seems correct syntax-wise, the error message seems a side-effect of the first error message.
where: CCKfieldname is the name of my field.
Suggestion
Don't call the CCK field validation function for custom PHP code outside a context of data manipulation (node/field). The validation function could still be called when defining a static default value (i.e. not custom PHP) in admin pages.
Comments
Comment #1
surgeonbor commentedAny updates on this? I'm running Drupal 6.9 with the current version of CCK and I have the same problem.
Comment #2
surgeonbor commentedForgot to update the version number. It's 6.x-2.1.
Comment #3
finex commentedI've a similar problem setting the default value on a text field. I'm trying to set the current username:
I've the same error:
where "struttura" is the name of the field and webmaster is the username of the user I'm using for configuring the field. I seems that when the field is saved, it partially compute the php code and something wrong happens...
Comment #4
finex commentedOk, maybe I've found why my code didn't work. Maybe it could help someone else. In my case the allowed values list of the select box doesn't contain the administrator username so the default value cannot be validated.
I've solved adding "webmaster" to the allowed value of values and after I've deleted it manually from the database.
Anyway a solution for doing this operation WITHOUT edit manually the DB could be better :-) :-) :-)
Comment #5
finex commentedP.S: Remember to clear the cache after removing the value from the DB.
Comment #6
hefox commentedSame issue
Just added the admin to the role to set the field
Comment #7
Ogredude commentedI think this might solve your problems. http://drupal.org/node/356589#comment-2030552