I'm trying to get User Reference to have a default value of the current user adding the content type. In the default value I added the code:

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

The attached shows the result with three different field types:

Contact One - User Reference [Autofill]
Contact Two - Text [Text Field]
Contact Three - User Reference [Check box]

Text works, but the User Reference won't allow the default value. Is this something done by design? Is there a work-around or is this just not possible?

Many thanks,

Adam

CommentFileSizeAuthor
cck_default_value.jpg14.35 KBadshill

Comments

yched’s picture

Category: bug » support
Status: Active » Fixed

Check the help text below the 'default value PHP' input. It explains that the expected format for userref fields is

return array(0=> array('uid' => $user->uid));
adshill’s picture

Status: Fixed » Active

Sorry, my mistake, I'm trying to get the username, not uid (as the attachment suggests) for which I was actually using the code:

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

As the image shows, the correct value (username: admin) appears on the text input, but not on the other two. Can you tell me what I'm doing wrong or if this is a CCK issue?

Thanks a lot,

Adam

yched’s picture

Doesn't change the fact that you need to provide the *uid*.
The 'default value PHP' box expects a return value in the *internal* storage format for the field-type. In the case of a userref field, this is the format I mentioned in comment #1.
Specifying a 'value' entry will have no effect.

adshill’s picture

Title: User reference default value not working » User reference default value to be the username of the current user

ok... my apologies for wrongly blaming this on the code, my logic is often different to php's :)

I assumed that since this module is designed to give a user reference it would be the place to find out how I would default the current user into the field. I also assumed that the php for doing this was pretty straightforward for coders and so wouldn't be hard to supply me with.

Anyway... thanks for your help.

I've changed the issue title in the hope someone else has an idea how to do this - as I've scoured the forums for the last 24 hours and the only solution I could find that almost worked was the one above.

yched’s picture

Status: Active » Fixed

We don't seem to understand each other.

This is the code you want to put in the 'default value' > 'php code' textarea :

global $user;
return array(0=> array('uid' => $user->uid));

This will make it so that on node *creation* form, the field is prefilled with the current user. Just checked, it works as intended on my setup.
If it doesn't work for you, then provide a detailed description of how your field is configured, what widget it uses, etc...

adshill’s picture

heh... damn me and my late nights. Sorry yched, I wasn't understanding... and it works like a dream.

Thanks so much!

Status: Fixed » Closed (fixed)

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

njehlen’s picture

Status: Closed (fixed) » Active

Any way for this to work on content editing forms as well as creation forms?

I have a site where I'm asking people to sign up to be responsible for a node that has already been created - they are taken to that node and I'd like it to automatically select the current user in the user reference field.

yched’s picture

Status: Active » Fixed

@njehlen: nope. CCK 'default value' only comes into play on node creation. Once the node is created, it is assumed that the values it has are intended and we don't mess with them.
You can always add custom specific behavior using hook_form_alter() from a custom module.

Status: Fixed » Closed (fixed)

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

SanDiego’s picture

I also created the user-reference field as above. But when I enter the suggested code:

global $user;
return array(0=> array('uid' => $user->uid));

I get the following error:

* Contact: invalid user.
* The PHP code for 'default value' returned Array ( [0] => Array ( [uid] => 1 ) ) , which is invalid.

The core is 6.13.

Thanks...

kardave’s picture

Status: Closed (fixed) » Active

Same bug here. It refuses the use of uid 1.

My solution was:

global $user;
if($user->uid!=1){
 return array(  0 => array('uid' => $user->uid) );
}
else return array();

This way the field is empty for administrator, but works great for other users.
I don't know if it is a bug or feature. If it is the latter, documentation is needed...

KarDave

markus_petrux’s picture

Status: Active » Closed (fixed)

@kardave: if you're suggesting what yched described does not work, then please re-open with as clear as possible step by step instructions to reproduce the issue.

This issue was started as a bug report, but it really was misunderstanding on how it works, so the issue became a support request, not a bug, and the doubt was already addressed. Just in case, I double checked, and what yched describes works. If it does not for someone, then please double check the format and what needs to be filed where.

thinkling’s picture

I saw the same behavior and found this thread.

On my site, each time I edit the content type, the field "User status that can be referenced" reverts back to "Blocked" even if it was set to "All users". If you don't notice that, and try to save the default value code, it will be refused with the error mentioned because the admin is not a blocked user.

If others see this behavior too then it may be worth filing an issue on the behavior of the user status field.

(Edit: running D6.14 with CCK 6.x-2.x-dev, Date 6.x-2.4, Views 6.x-2.6, some other small modules.)

markus_petrux’s picture

@maartenvd: Oh, I see. It is related to the "User status that can be referenced" option!

I have just filed a separate issue about this bug. Please, see #585048: Setting "All users" in "User status that can be referenced" option reverts to "Blocked users".

ressa’s picture

Thanks kardave, your solution fixed the problem for me.

NOTE: yched's solution only works if the administrator role is checked under "User roles that can be referenced". You have to install admin role to be able to do this.

finex’s picture

Thanks for the solution.

betsyd’s picture

So, is there a way to reference the uid of the referenced node's author (not the current logged-in user). Would you do this with a node reference?

Thanks.

nodecode’s picture

+1

@betsyd: I'm trying to figure out the same thing regarding obtaining the UID of the REFERENCED USER... http://drupal.org/node/815868