Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 29th, 2012  |  syntax: None  |  size: 1.51 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Autcomplete fields with database in Drupal content type
  2. function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
  3. {
  4.     if($form_id == "YOUR-CONTENT-TYPE_node_form")
  5.     {
  6.         $form['field_YOUR-FIELD']['und'][0]['value']['#autocomplete_path'] = 'my-module/autocomplete/path';
  7.     }
  8. }
  9.        
  10. function YOUR_MODULE_menu()
  11. {
  12.     $items = array();
  13.     $items['my-module/autocomplete/path'] = array(
  14.     'page callback'     => 'your_module_autocomplete_callback',
  15.     'access callback'   => TRUE,
  16.     'weight'            => 1,
  17.     'type'              => MENU_CALLBACK,
  18.     );
  19.     return $items;
  20. }
  21.        
  22. function your_module_autocomplete_callback($string)
  23. {
  24.     $items = array();
  25.     $query = db_select('node', 'n');
  26.     $value = $query->fields('n', array('title'));
  27.     $value = $query->condition(db_and()->condition('n.type', 'YOUR_CONTENT_TYPE')->condition('title', '%' . db_like($string) . '%', 'LIKE'))->orderRandom()->execute();
  28.  
  29.     $i = 0;
  30.     foreach ($value as $val)
  31.     {
  32.         $items[$val->name] = check_plain($val->name);
  33.     }
  34.     print drupal_json_output($items);
  35.     exit();
  36. }
  37.        
  38. function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
  39. {
  40.     if($form_id == "YOUR-CONTENT-TYPE_node_form")
  41.     {
  42.         foreach($form['field_match']['und'][0]['field_adversaires']['und'] as $k =>$v)
  43.         {
  44.             if(is_numeric($k))
  45.             {
  46.                 $form['field_match']['und'][0]['field_adversaires']['und'][$k]['value']['#autocomplete_path'] = 'basketfacile_type/autocomplete';
  47.             }
  48.         }
  49.     }
  50. }