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
| Comment | File | Size | Author |
|---|---|---|---|
| cck_default_value.jpg | 14.35 KB | adshill |
Comments
Comment #1
yched commentedCheck the help text below the 'default value PHP' input. It explains that the expected format for userref fields is
Comment #2
adshill commentedSorry, my mistake, I'm trying to get the username, not uid (as the attachment suggests) for which I was actually using the code:
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
Comment #3
yched commentedDoesn'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.
Comment #4
adshill commentedok... 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.
Comment #5
yched commentedWe 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...
Comment #6
adshill commentedheh... damn me and my late nights. Sorry yched, I wasn't understanding... and it works like a dream.
Thanks so much!
Comment #8
njehlen commentedAny 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.
Comment #9
yched commented@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.
Comment #11
SanDiego commentedI 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...
Comment #12
kardave commentedSame bug here. It refuses the use of uid 1.
My solution was:
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
Comment #13
markus_petrux commented@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.
Comment #14
thinkling commentedI 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.)
Comment #15
markus_petrux commented@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".
Comment #16
ressaThanks 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.
Comment #17
finex commentedThanks for the solution.
Comment #18
betsyd commentedSo, 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.
Comment #19
nodecode commented+1
@betsyd: I'm trying to figure out the same thing regarding obtaining the UID of the REFERENCED USER... http://drupal.org/node/815868