Hello,

I'm building a small backoffice for a client and I've added a field 'User Reference' in my node type.

The client would like to be able to select user by their email address, or any other fields of the user profile.

Do you have an idea on how I could do this ?

Thanks!

Comments

pol’s picture

The User Reference Module only look into the username, which is unique in the user db.

I think being able to search a mail address should be mandatory.

Some others customs fields could be interesting to search into like phone numbers, name, firstname... etc etc.

I'm ready to do it, but I'll need a bit of help from the developpers.

patcon’s picture

Are you familiar with the Advanced views-dependant setting for nodereference and userreference? It should show up at the bottom of the specific field reference page (if views is installed).

The idea is that you can design a view table whose rows will show up in the nodereference autocomplete dropdown. The catch is that, while you can make the autocomplete show pretty much any combination of fields that Views can muster, it ONLY searches the title field.

So if you had some a content type called "Staff member" with the Title field labelled as "Name", and 2 more CCK fields for "City" and "State", then you could build a view that made the autocomplete show up as "Sam Jones (Boston, MA)". The downside is that You have to start typing "Sam" or "Jones" to find it (not "Boston").

There's another module called Finder that can search cck fields, but only one at a time, so it's sort of the same problem.

BUT there are several workarounds:

  1. Use the Automatic Nodetitles module and tokens to generate a title containing all the fields you'd want to search by. Make an effort not to list this node by title in any other views, as it will be messy. Then you can build an advanced nodereference view as mentioned above. There's a bug that requires you to add the Title field to the view, but just check the box to exclude it. This would come into play is you had the city, state, phone, full name, email in the node title (so that it was searchable), but you only wanted to autocomplete to list "Sam Jones (Boston, MA)" as the suggestion.
  2. Use the Finder module and the Computed field module in pretty much the same way as listed above. Using tokens, build a computed field which includes every other field you'd want to search by. Search by that "master" field, but exclude it from the display and create a prettier set of fields to display in Finder. You can even include images, which is nifty. Also, you don't have to worry about the messy title anymore, and you can hide the ugly computed cck field so that it's not visible anywhere else (just used to search).
  3. Finally, some dude named kswan created an unofficial module called Super Nodereference that allowed nodereference to search all fields and not just title:
    #375690-14: Nodereference autocomplete doesn't match all of view's fields
    The downside is that the module is likely unsupported, and might break during a cck or views upgrade. The upside is that D7 is looking to fix this whole nodereference-title pitfall, so these hackish means shouldn't be needed anymore if things go as planned.

Hopefully this helps! Haven't tried all the methods myself, but they are technically possible :)

pol’s picture

It's a bit sad that Drupal is so advanced in some domains and so poor in some others, like this one.
For me, it's quite important to be able to search each field.

I'm currently browsing the forum for a solution and nothing, am I the only one to need this ?

Thanks for your reply, but I think I should start to hack the user reference module and adding some stuff in it, at least, I'll try.

roball’s picture

Priority: Critical » Minor
patcon’s picture

Sorry Pol, but for all intents and purposes (as in, as far as your users are concerned), it IS possible. I wanted to present all options, but put simply, just use computed field with tokens to add all fields to one giant all-encompassing field, then use finder to search that field in a view. Hide that master field from the full node and teaser so that it never shows on node view. As for the view that finder uses for autocomplete, make it pretty using the original fields, and then include the master field as the "primary" searchable field, but exclude it from display with the appropriate checkbox.

Give it a shot -- it should work. Easy peasy :)

patcon’s picture

Oh... sorry, if it's not too late, use content profile, not profile field. Profile is on its way to obsolescence as far as I can tell. Use that and you profile will be a node with cck field, which is much more versatile and can be used with the aboce method. Content profile can do pretty much anything profile can do, including prompting for field entry during registration. Email me if you need any help

pol’s picture

Thanks for your suggestion dude :)

I'll do that and report here if I need help, maybe that will help someone else too :)

But first, the client need to sign the contract ! :)

patcon’s picture

Ack! I finally played around with Finder this weekend and realize that I misunderstood how it worked -- it's a search replacement, not a node reference replacement. There's still the other option though!

Good luck!

pol’s picture

I tried to use the computed field and Content profile.

It works, I made a field who concatenate all the values from others fields, that's great.

Now I'm looking for doing a view with a filter: user reference who can find users based on that field... I don't find.

Any idea ?

patcon’s picture

Hi Pol, let me just make sure I've got you straight:

Do you want to REFERENCE a user (as in a userref or noderef field), or SEARCH for a user (to pull up their details)? Or is it both? Because the solution isn't the same.

Currently, exposed filters don't support autocomplete (although it's being discussed in places), so searching a view like that isn't doable. If that's what you want to do, then the Finder module is your best bet :)

pol’s picture

Hi Patrick,

I would like to include a field who can search through my users (all their fields, using Content profile) if possible.

If it's not possible, having the ability to search also in email field and other custom fields of the profile (stock profile module) rather than looking only in the field username.

I check your links right now. Thanks!

lsiden’s picture

Patrick, I have a similar problem. I want to create a field that links to a user-ref, but I want it to be searchable by the user's full name (e.g. "John C. Smith). After reading the above thread, I
1) created a new content type "Personal Info" (actually re-using the "profile" type created by the Profile module, which is now disabled), and created a new field for it, "Full Name".
2) Then, using auto-title, I set the title of each node to be the contents of the Full Name field.
3) Then I created a view that returns the Full Name and uid for each node of type Personal Info.
4) I set the filter to match nodes of type Personal Info belonging to an active user and whose title matches the exposed filter input.
5) In a different node-type, Customer, I created a node-reference field and pointed it to this view.

When I display the node/add/customer page and type "John", in the drop-down it displays "testuser01 - John C. Smith". When I select it, it displays "John C. Smith [nid:423]".

Workable, but ugly! How can I tame this to display only the user name in the auto-complete drop down? Once I get that, all I care about is the uid of the user, so I'd like to save that to a hidden field that will upload with the post. Is there a way to accomplish that?

lsiden’s picture

FWIW, I used Patrick's suggestion #1. My goal was to provide a widget that would allow a visitor to optionally select a sales rep by the rep's full name (e.g. "Chuck Jones", instead of "chucky56") when the visitor registers a new account for himself, then record the sales rep's user-id when the visitor submits the form. There are several problems that make this non-trivial:

1) There is no full-name field in a $user object. That has to be created either in a Profile node or a content_profile node. I started out by enabling the Profile module, then adding a "fullname" field, but then I disabled it and enabled the content_profile module and set the 'profile' content-type as a content-profile type.

2) You can't use the CCK userreference field widget to search on content-profile nodes. It will only search on usernames (e.g. "chucky56"). If you use a custom view for autocomplete, it will just put the output of that view in the form field. For example, "Chuck Jones - [uid: 289]". That's workable, but ugly. Also, the user can tamper with it before submitting the form making the value meaningless, or worse!

3) If you define a field as a CCK nodereference and use the CCK nodereference field widget with a custom view you get much the same problem. You want the user to see people's full names when the field autocompletes, but you really want to submit only the user-id of the selected user. Besides, person's names aren't unique in this world, unless your's is "Lady Gaga"!

So to get this to work I had to go through several hoops:
1) I defined a simple CCK number field, called sales_rep_uid.
2) In hook_form_alter(), I added textfield called "sales_rep_uid_autocomplete" with an #autocomplete_path property.
3) The autocomplete call-back returns an array of [ {uid: fullname}, ... ]
4) I had to copy and redefine some of the Javascript from misc/autocomplete.js and modify it to put the uid of the selected entry in the number field when the user selects a name.
5) I set the style of the number field to "display: none". I would have preferred to just redefine it as a "hidden" field in hook_form_alter() but this caused PHP errors when I tried to submit the form, so I fell back on hiding it with CSS.
6) Finally I over-rode the preprocess function for content-field to display the number field as a link to the contact page for the sales rep.

All in all, it took about a day's work to figure it all out and get it to work. I'm sure there's a better way to do it. There are probably ninja's out there who can do it while they take a bathroom break, and do it better, but I'm not there yet. I thought about being a hero and contributing a new CCK widget that would do all this, but it would probably take me several days to a week generalize a solution and test it and work out all the kinks. I've barely scratched the surface of hacking the CCK. I'm staring a new gig on Monday, plus I'm still working on this one so I don't think I have that kind of time. But if anyone wants to work with me on it, I guess I'm still a sucker, because I love doing this.

I'm glad someone is on this for D7. Maybe that's the best strategy for now.

fuzzy76’s picture

It looks like patcons alternative 1 does not work out of the box. I can't get it to search my content profile node titles. :( Anyone got some pointers, or able to take a look at my view?