User reference in custom module

Events happening in the community are now at Drupal community events on www.drupal.org.
kristat's picture

I am working on a custom module that requires a user reference field.
I have not had much luck finding information on this.
Please let me know if anyone has experience doing this and can point me in the right direction, or if it can be done.
Thanks in advance

Comments

CCK

glass.dimly's picture

CCK has a submodule in D6 for User Reference.

Not using cck as it is a

kristat's picture

Not using cck as it is a custom module i am building and I need all the data to go to one table

Krista Trovato
http://pdiwebdesign.com

I'm not sure if you mean you

Nikki Aiuto's picture

I'm not sure if you mean you need a custom module or just a custom content type. For the latter, CCK provides all you need (as glass.dimly indicated).

If you have not installed the CCK module and enabled the User Reference module (as glass.dimly indicated), you will not see the option to create fields that reference users. Once you do that, you can create a new content type and create a new user reference field which will allows users to reference an existing user via a select list, check boxes or autocomplete text field.

more details please

Nikki Aiuto's picture

Which 'data' do you mean? Perhaps you can give us some idea of the functionality you are trying to implement.

data from the form

kristat's picture

data from the form fields.
Back to cck I guess

Krista Trovato
http://pdiwebdesign.com

I am creating a simple custom

kristat's picture

I am creating a simple custom module that is intended to track hours for users. The module includes a new content type developed in the .module. The content type requires a user reference thst needs to be written in the .module file not added via cck. I know how cck works and have used that user reference field, but now i need to build my own so that i can have the data write to the tracking table associated with the custom module not to the field table created by cck.

Krista Trovato
http://pdiwebdesign.com

I would re-architect it to

glass.dimly's picture

I would re-architect it to use CCK, but if you don't want to do that, why not look at CCK's User Reference module and copy parts of its functionality?

The problem is that you are

glass.dimly's picture

The problem is that you are writing from scratch what has already been written in CCK. The Drupal Way is to work with existing modules.

I already implemented the

kristat's picture

I already implemented the functionality with cck, but it did not satisfy the client requirements. I guess I will go back and see if I can make that work in cck again, since selecting users is also part of the requirement. It seems like a user list in a custom module would be a no brainer, but it is not..
I am starting to think the client is asking for something that can not be done in drupal. And explains why I was never able to find a volunteer tracking module in my searches.
Back to the drawing board I guess.

Krista Trovato
http://pdiwebdesign.com

Some additional thoughts

therzog's picture

It seems like a user list in a custom module would be a no brainer, but it is not.

Most people do user lists with the userreference module

I already implemented the functionality with cck, but it did not satisfy the client requirements.

Maybe you should share what the requirements are? That would help us understand why userreference isn't working for you, and why you are trying hard not to use CCK.

I am starting to think the client is asking for something that can not be done in drupal.

Again, tough to say without knowing what the client is asking for.

Keep in mind you can just use the plain old form API if all you're looking for is a front-end to display a user list, allow them to pick, and store the result in a field. For example:

$options =array();
$result = db_query("SELECT uid,name FROM {users} WHERE status=1 ORDER BY name");
while( $obj = db_fetch_object($result) ) {
  $options[$obj->uid] = $obj->name;
}

$form['myfield'] = array(
  '#type' => 'select',
  '#options' => $options,
  '#default_value => '[value from node]',
);

Or you could use a field of '#type' => 'textfield' with the #autocomplete_path attribute; see http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....

You are awesome. I at least

kristat's picture

You are awesome. I at least got the field AND it is populated correctly. Not sure if I will be able to wrap it up the way I want, but after 16 hours of struggling with this THANK YOU. At least I can move on now.
I tried this over and over, but yours worked. Again Thank you

Krista Trovato
http://pdiwebdesign.com

Kristat, I'm still not fully

michaelscappa's picture

Kristat,

I'm still not fully sure why you can't use CCK user_reference for this, but if you really can't (maybe you have a table structure already defined?) you can simply use form_api and populate your users as #options to a #type=>'select'. Then on your submit just save them to the table you are trying to save to.

The trouble I had with cck is

kristat's picture

The trouble I had with cck is that I need to keep a running total of each users hours, but they users all have to be able to chosen in one form and submitted at once. This caused the users to be treated a group.
So, if Sue, Tom and Joe each worked 3 hours, that data is stored in for those 3 users as a group. I need to be able to loop through and save each users info separately to be able to come up with a total for only Sue rather than the group.
The user entering the hours needs to be able to select as many users as necessary, but the information has to be written to each user as well as the group, so that eventually I would know that Sue had 10 hours total and Joe had 6 hours total. All in all the groups hours are irrelevant.
There is perhaps an easy way to do this that I am missing, so any advice would be great.

Krista Trovato
http://pdiwebdesign.com

Ah, I see. I had a project

therzog's picture

Ah, I see. I had a project last year like that. What you need is the equivalent of "compound" CCK field, where each field can hold at least two values, the user ID and the hours worked, and than configure for multiple user/hour fields per node.

The way I solved it was to extend the CCK module with my own field type, which had two database fields per value. Then you need to create a form widget to support form entry and value loading and saving. Several CCK modules (such as FileField or Location CCK) take this approach, so if you're interested, you can look at one of those for a template. I could send you the code for mine, but it is Drupal 5.

Hope that helps,
Tim

cck fieldgroup

michaelscappa's picture

cck fieldgroup

cck_fieldgroup would allow

therzog's picture

cck_fieldgroup would allow you to create a group of fields within a form. It would not allow you to intuitively manage multiple, compound fields, like this:

[uid] [hours]
[uid] [hours]
[uid] [hours]

Where keeping the user/hours relationship is important.

my mistake... I meant cck

michaelscappa's picture

my mistake... I meant cck multigroup...

I think I understand what you

michaelscappa's picture

I think I understand what you are going for...

Sounds to me like you could to use a linking node type that has a user_ref, cck integer field for hours, and a node_ref to the parent node.

(main parent node)
-> Add hours (linking node add form)
-> Select user from list, enter number value, node_ref for parent populates automatically (see prepopulate module)

Good luck with your project

Lou T's picture

Hope this client gets their project completed. Still waiting for mine from you

Washington, DC Drupalers

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: