Download & Extend

Checkbox to use Current User as the field default value

Project:Content Construction Kit (CCK)
Version:6.x-2.7
Component:userreference.module
Category:support request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

Hello,

Thanks to everyone in this great community. I am new to Drupal and make my first steps in understanding.
So far I could figure out every solution with the infos I found here. But not in this case.

I use CCK and the USER REFERENCE modul/field. When I enter the configuration of that field in my content type I see the option 'Default Value' and the suboption to enter php code to define the default value.
That is exactly what I need (I think).

What code would I have to enter there to select by default the currently logged in user.

Can anybody help me with this code ? I have no clue about php

Below the PHP default value entry field there is the following hint:

"Advanced Usage Only: PHP code that returns a default value. Should not include

<?php

?>
delimiters. If this field is filled out, the value returned by this code will override any value specified above. Expected format :

array(
0 => array('uid' => value for uid),
// You'll usually want to stop here. Provide more values
// if you want your 'default value' to be multi-valued :
1 => array('uid' => value for uid),
2 => ...
);

Using devel.module's 'devel load' tab on a story content page might help you figure out the expected format."

Thank you so much ... I hope I learn fast to be able to contribute back to the community soon ...

Comments

#1

This works:

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

#2

And what about autofilling additional "profile_" -entries.

Example:

Registered user has profile information:

first name(profile_firstname)
last name(profile_lastname)

How can i make for example the first name a default field value?

Tried several approaches but the field stays empty :(

#3

Title:CCK > user reference field > default value > php code to choose currently logged in user from the list» Checkbox to use Current User as the field default value
Project:User Referral» Content Construction Kit (CCK)
Version:5.x-1.x-dev» 6.x-2.7
Component:Miscellaneous» userreference.module
Assigned to:mo_» Anonymous
Status:postponed (maintainer needs more info)» active

A word of caution - the person *creating* the field has to have any roles that are defined as requirements on the field, otherwise saving the field definition fails the validation code. Even when you are user 1.

There really should be a simple checkbox in the Default Value block to select the current user as the default.

#4

This is super helpful--

I have a modification of the question:

how can i FORCE the node being created to be user referenced to a certain user id..

for example-- i may want certain users to only be able to reference themselves or other users in a group or category i make up..

Using the code from #1 works to set the default value, but what if i want to set the allowed values?

thanks