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

  1. Logged in as admin, configure a CCK User Reference field.
  2. 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
  3. Go to admin setting: Widget > Default value > Php code
  4. 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));
    
  5. Go to admin setting (same page): "User roles that can be referenced"
  6. 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

surgeonbor’s picture

Any updates on this? I'm running Drupal 6.9 with the current version of CCK and I have the same problem.

surgeonbor’s picture

Version: 5.x-1.6-1 » 6.x-2.1

Forgot to update the version number. It's 6.x-2.1.

finex’s picture

I've a similar problem setting the default value on a text field. I'm trying to set the current username:

global $user;
return array(
  0 => array('value' => user_load($user->uid)->name ),
);

I've the same error:

* Struttura: illegal value.
* The PHP code for 'default value' returned Array ( [0] => Array ( [value] => webmaster ) ) , which is invalid.

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...

finex’s picture

Ok, 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 :-) :-) :-)

finex’s picture

P.S: Remember to clear the cache after removing the value from the DB.

hefox’s picture

Same issue

Just added the admin to the role to set the field

Ogredude’s picture

I think this might solve your problems. http://drupal.org/node/356589#comment-2030552