Search CCK Name field
Zahor - December 11, 2007 - 02:29
| Project: | Message Box |
| Version: | 5.x-1.x-dev |
| Component: | User interface |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Jump to:
Description
Hey I was wondering if I could search a CCK name field vs searching user names in the autocomplete field. If so, how would I go about doing this?

#1
you will need to do two things:
- change the autocomplete function in the module to return you the list of cck name fields
- figure out a way to map the cckname field to the uid field - that may be tricky if there is more than one person with the same name in the send message function.
since there is no standard way to guarantee that the cckname field is unique and also having a unique method to mapping back to the uid of the user - this is not a trivial task - if you are able to recommend a decent approach I am willing to consider adding this functionality. Recognize that one is free to define their CCK node type and fieldname anyway they want to so we cannot tie in this module to only lookup one CCK content type and fieldname.
#2
Yea the first part I was able to do, but mapping it to the uid I can't figure out . I imagine there has to be a series of JOINs but I don't really know how to do that very well.
I realize that people who have different fields, so if that was added there would have to be a note in the readme or something with the code.
#3
I got this to work (thanks to this post (http://drupal.org/node/2041)). You search their full names, and when you click to add it, their user names are added. Here is the code I used:
function messagebox_autocomplete($string) {
$matches = array();
global $user;
$names = explode(',', $string);
for ($i = 0; $i < count($names); $i++) {
$names[$i] = trim($names[$i]);
}
$search = array_pop($names);
if ($search != '') {
$sql = "SELECT cfn.field_name_value, u.name FROM {content_field_name} cfn INNER JOIN {node} n ON n.nid = cfn.nid INNER JOIN {users} u ON u.uid = n.uid WHERE LOWER(field_name_value) LIKE LOWER('%s%%') AND u.status <> 0";
$result = db_query_range($sql, $search, 0, 10);
$prefix = count($names) ? implode(', ', $names) .', ' : '';
while ($user_names = db_fetch_object($result)) {
$matches[$prefix . $user_names->name] = check_plain($user_names->field_name_value);
}
print drupal_to_js($matches);
exit();
}
}
So if someone else were using this they'd just have to change all occurrences of "cfn" (3 times)to the abbreviation of their table name, "content_field_name" (1 time) to the name of the table with their Full name and "field_name_value" (2 times) to the name of the row in the table that stores full names.
#4
thanks jahor - I will include this as a optional addin