Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By upupax on
I was looking for the autocomplete CCK widget in Drupal 7, but I've seen that it's available for taxonomy only.
Is there a way to have it again?
I don't want a vocabulary for my field.
Comments
Why don't you want a
Why don't you want a vocabulary?
Cause they are just time slot
Cause they are just time slot (i.e. 10:00-10:45, 10:00-10:30).
I don't need them to be categorized, and I don't want drupal generates a page for each term.
You could use a list (text)
You could use a list (text) and a select list for the element.
I could, but then I should
I could, but then I should manually add values from the backend to let user select them.
With autocomplete, new values would be automatically added.
Auto complete in this case
Auto complete in this case would need a custom input (it needs somewhere to store the existing data).
Sorry, I was totally wrong. I
Sorry, I was totally wrong.
I thought drupal 6.x had autocomplete for simple textfield too, but it didn't.
I've checked a site with a previous version, and neither there I could find this option.
So, I think I only dreamed about it! :)
Autocomplete CCK text field or any form field ( Solution)
At present no module is available for CCK autocomplete field but you can achieve this by custom code :
Step 1 : use hook_form_alter((&$form, &$form_state, $form_id)
create case for your form_id
Step 2 : Add '#autocomplete_path' to the field where you want to add autocomplete functionality.
take reference of code given below :-
$form['autocomplete_textfield'] = array(
'#title' => t("Autocomplete Textfield"),
'#type' => 'textfield',
'#autocomplete_path' => 'autocomplete/example/textfield'
);
Step 3 :- and in hook_menu() :-
$items['autocomplete/example/textfield'] = array(
'page callback' => 'autocomplete_example_textfield',
'access callback' => TRUE,
'weight' => 1,
);
Step 4 :- now define the callback function.
function autocomplete_example_textfield ($string) {
$items = array();
$query = db_select('node', 'n');
$value = $query->fields('n', array('nid', 'title'));
$value = $query->condition(db_and()->condition('n.status', 1)->condition('title', '%' . db_like($string) . '%', 'LIKE'))
->orderRandom()
->execute();
$data = array();
$i = 0;
foreach ($value as $val) {
$items[$val->nid] = check_plain($val->title);
}
print drupal_json_output($items);
exit();
}
I hope this will help you.
Good Luck !
Cheers !
Thanks :)
Your post was very helpful, thanks!
Of course one could use a
Of course one could use a node reference field from the references module.
Autocomplete Widgets for Text and Number Fields
For those who visit this page by Google, see: http://drupal.org/project/autocomplete_widgets