By john_the_noob on
Hi,
I have a hard time to create a new custom autocomplete path so i can use form_alter with autocomplete. See my OP here: http://drupal.org/node/970856
The task: Explain how to create a new autocomplete path that retrieves all results for a random cck value or a random value from the DB. (second might be better)
I´d like to post the results here, so everybody can benefit. (This will be usefull for many people)
The Bountie is 33$ and will be send via Paypal or regular Bank transfer.
Cant´t wait to see the results, John
Comments
add code
I'm not sure this works, is difficult to standardize the function for each cck type
in form_alter (define your_cckfield)
if this does not work at all, it can give you an idea
Juan
Hi, You are right it is
Hi,
You are right it is difficult to standardize the module however here is the .module and the .info file with a slight modification of the above which lists 10 values starting with the entered letters. I have also kept it comma seperated like taxonomy tags. I hope it is correct :-)
here is the .info file
Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form
wow, that looks great. will
wow, that looks great. will try it when back home :)
Doesnt´t work yet. There is
Doesnt´t work yet.
There is an autocomplete widget and it performs a search but it is always empty. cck_field/autocomplete/your_cckfield/x always returns an empty array. Tried different values and different cckfields but nothing.
I changed only the cck_identifier and my_cck fields in form_alter as this module should make a path for every cck_field, yes ?
change your_cckfield
your_cckfield is your cck field name, you should change this(eg: my_image), I'm not sure the added code:
Juan
Hi Juan, sure.
Hi Juan,
sure. I use cck_field/autocomplete/thename_from_cck, then i try cck_field/autocomplete/thename_from_cck/a just to see if it returns results beginning with 'a'. The first code returns 'null' (long search), the modified '[]' (short search).
Try this _cck_fields_autocomplete
your cck_field without "field_" and in type script
Juan
You tried this ? i Get Parse
You tried this ? i Get
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting T_STRING or T_VARIABLE or '{' or '$'..
in line: $matches[$edata->"field_" . $cck_fields . "_" . $aux1] = check_plain($edata->"field_" . $cck_fields . "_" . $aux1);
But can´t see the typo.
fix code
function _cck_fields_autocomplete($cck_fields = '', $string = '') {
$query = "SELECT type, multiple FROM {content_node_field} WHERE field_name like 'field_%s'";
$result = db_query($query, $cck_fields);
$data = db_fetch_array($result);
$aux1 = 'value';
if ($data['type'] == 'filefield') {
$aux1 = 'fid';
}
if ($data['type'] == 'nodereference') {
$aux1 = 'nid';
}
if ($data['type'] == 'userreference') {
$aux1 = 'uid';
}
if ($data['type'] == 'link') {
$aux1 = 'url';
}
if ($data['type'] == 'email') {
$aux1 = 'email';
}
if (!$data['multiple']) {
$query = "SELECT field_'%s'_'%s' FROM {content_type_'%s'} WHERE field_'%s'_'%s' like '%s%'";
}
else {
$query = "SELECT field_'%s'_'%s' FROM {content_'%s'} WHERE field_'%s'_'%s' like '%s%'";
}
$result = db_query_range($query, $cck_fields, $aux1, $cck_fields, $cck_fields, $aux1, $string, 0, 10);
$fields_aux = "field_" . $cck_fields . "_" . $aux1;
while ($edata = db_fetch_object($result)) {
$matches[$edata->$fields_aux] = check_plain($edata->$fields_aux);
}
// return for JS
print drupal_to_js($matches);
exit();
}
Hi Juan, thx for your quick
Hi Juan,
thx for your quick reply. Typo is gone. But cck_field/autocomplete/cck_field_name always returns 'null' regardless if there is a parameter or not or if i use field_cckname or just cckname.
I am sorry that this takes so long !
debugging help
Maybe this helps: I added
$matches[0] = 'test';
before
// return for JS
print drupal_to_js($matches);
and it now always returns test. So that part of the code works for sure.
Maybe sql problem
Try change
to
OR TRY
see output, I can't try this now
meh
$matches[] = "TEST"; adds TEST as expected.
Adding it inside while ($edata = db_fetch_object($result)) returns null.
fix function _cck_fields_autocomplete
function _cck_fields_autocomplete had many mistakes, I've fixed this and seems to work
Juan
Juan, you are fucking awesome
Juan, you are fucking awesome and clearly deserve the bounty !
(pm me your e-mail for paypal please)
Now i have to find out how to retrieve taxonomy tags by node type ;)
So the final working code was Juan's last post?
Or was that just part of it? What was needed to get it working on your end?
For a exposed filter form with multiple text fields that I wanted to use auto-complete would I need to roll this into separate functions for each field?
Thanks much,
Jared
use form_alter function
No, you don't need separate functions, only you can use form_alter and change fields in form (see http://drupal.org/node/970856 in this post), for each field that yo want change EG:
hoping to help
Juan
Subscribe
Subscribe