0

I am trying to implement an auto-complete text field:

hook_menu()
{
  $items['products/autocomplete'] = array(
    'page callback' => 'json_list_products',
    'access arguments' => array('access products')
  );

  return $items;
}

This appears to work but when I access the products/autocomplete I see the resulting JSON data but it's embedded in the HTML template?!?! This is going to cause part of my problem

I am generating the form like so:

function form_search_products($form, &$form_state) {

  $form['keyword'] = array(
    '#type' => 'textfield',
    '#description' => 'Enter keywords (ie: Fat free milk)',
    '#autocomplete_path' => 'products/autocomplete' 
  );  

  return $form;
}

And finally the json query function:

function json_list_products() {

  $keywords = 'fat free milk';   // TODO: Swap keywords from parameter list from HTML on client

  $items = array();
  foreach(_sql_select_products($keywords) as $product) {
    $items[$product['Product_ID']] = $product['Product_Name'];
  }    

  return drupal_json_encode($items);
}

Anyway, nothing seems to happen...i've managed to do this before so I am not sure why this isn't working now. Any ideas? json_list_products() currently hardcodes the keywords and the results are correct.

Alex

4
  • posting the json_list_products function will help
    – 2pha
    Aug 7, 2013 at 12:52
  • Replace return with print in your Ajax callback and it'll start working
    – Clive
    Aug 7, 2013 at 13:07
  • That solved the JSON being exclusively returned but the input still doesn't show any sign of auto-complete :( Aug 7, 2013 at 13:09
  • The little circle in the textfield isn't showing up for some reason so I know something is still a miss :s Aug 7, 2013 at 13:22

3 Answers 3

1
function modulename_menu() {
  $items['products/autocomplete'] = array(
  'page callback' => 'json_list_products',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK
  );
 return $items;
}

function form_search_products($form, &$form_state) {
  $form = array();

  $form['keyword'] = array(
  '#type' => 'textfield',
  '#description' => 'Enter keywords (ie: Fat free milk)',
  '#autocomplete_path' => 'products/autocomplete' 
  );  

  $form['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Save',
  );

  return $form;
}




function json_list_products($string) {
  $matches = array();

  // DB table which holds products
  $query = db_select('products', 'p');

  // Select rows that match the string
  $return = $query
  ->fields('p', array('product'))
  ->condition('p.product', '%' . db_like($string) . '%', 'LIKE')
  ->range(0, 10)
  ->execute();

  // add matches to $matches
  foreach ($return as $row) {
    $matches[$row->product] = check_plain($row->product);
  }

  drupal_json_output($matches);
}
0

In your hook_menu you need to set the type to menu_callback as the default is MENU_NORMAL_ITEM.

hook_menu()
{
  $items['products/autocomplete'] = array(
    'page callback' => 'json_list_products',
    'access arguments' => array('access products'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

Also, you may want to use the drupal_json_output function instead of drupal_json_encode in your json_list_products function. the drupal_json_outpur set the correct header.

Also make sure you have the access products permission set correctly.

Not sure if these things will fix your problem, but it's all I can see.

1
  • Check, check and check none worked...I gave up and moved to a Twitter typeahead. Not sure how I close this but now i'm having different drupal issues :p thanks again :) Aug 7, 2013 at 15:42
0

I try this one. You must try this

hook_menu()
    {
     $items['products'] = array(            //say you have a menu 
     'page callback' => 'drupal_get_form',
     'page arguments' => array('form_search_products'),
     'access arguments' => array('access products')
      );


      $items['products/autocomplete'] = array(
        'page callback' => 'json_list_products',
        'access arguments' => array('access products')
      );

      return $items;
    }


 function form_search_products($form, &$form_state){

  $form['keyword'] = array(
    '#type' => 'textfield',
    '#description' => 'Enter keywords (ie: Fat free milk)',
    '#autocomplete_path' => 'products/autocomplete' 
  );  

  return $form;
    }


function json_list_products($enteredstring){

$yourvalue = $enteredstring;

  $result = db_select('products', 'c')
    ->fields('c')
    ->condition('productname','%'.db_like($yourvalue) . '%', 'LIKE')
    ->orderby('productname')
    ->execute();
}

  $items= array();
  foreach ($result as $items) {
   $title=$items->title;
   $items[$title]=$title;
  }
drupal_json_output($items);

  }

It works well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.