
Untitled
By: a guest on
Jul 29th, 2012 | syntax:
None | size: 1.51 KB | hits: 22 | expires: Never
Autcomplete fields with database in Drupal content type
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "YOUR-CONTENT-TYPE_node_form")
{
$form['field_YOUR-FIELD']['und'][0]['value']['#autocomplete_path'] = 'my-module/autocomplete/path';
}
}
function YOUR_MODULE_menu()
{
$items = array();
$items['my-module/autocomplete/path'] = array(
'page callback' => 'your_module_autocomplete_callback',
'access callback' => TRUE,
'weight' => 1,
'type' => MENU_CALLBACK,
);
return $items;
}
function your_module_autocomplete_callback($string)
{
$items = array();
$query = db_select('node', 'n');
$value = $query->fields('n', array('title'));
$value = $query->condition(db_and()->condition('n.type', 'YOUR_CONTENT_TYPE')->condition('title', '%' . db_like($string) . '%', 'LIKE'))->orderRandom()->execute();
$i = 0;
foreach ($value as $val)
{
$items[$val->name] = check_plain($val->name);
}
print drupal_json_output($items);
exit();
}
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if($form_id == "YOUR-CONTENT-TYPE_node_form")
{
foreach($form['field_match']['und'][0]['field_adversaires']['und'] as $k =>$v)
{
if(is_numeric($k))
{
$form['field_match']['und'][0]['field_adversaires']['und'][$k]['value']['#autocomplete_path'] = 'basketfacile_type/autocomplete';
}
}
}
}