3

I have the following hook_menu:

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

Here is the function:

function json_list_products($keywords) {

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

  drupal_json_output($items);
}

$keywords is not populated with the value of the % as i suspected - how do I populate this value?

Alex

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

view the documentation at this link

  • I have added the 'page arguments' as you have indicated...added an echo $keywords and exited from the json_xxx function and invoked the URI directly like so: domain.ca/products/autocomplete.json/cook Nothing happens...any ideas??? The documentation is very confusing and no newbie friendly :) EDIT | I refreshed the cache too – Alex.Barylski Aug 7 '13 at 16:02
  • instead of sing echo...try installing the devel module and using dpm($keywords). – 2pha Aug 7 '13 at 17:03
  • 1
    Figured it out stepping through drupal code...turns out I had 'page argument' not 'page arguments' UGH!!! Thanks again everyone :) – Alex.Barylski Aug 7 '13 at 17:17
  • Sure thing, sorry boss :) – Alex.Barylski Aug 8 '13 at 17:38
2

Try this to start:

function mymodule_menu() {
  $items['products/autocomplete.json/%'] = array(
    'page callback' => 'json_list_products',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function json_list_products($keywords) {
  drupal_json_output($keywords);
}

Note the following:

  • Set page arguments to position in your path where you expect the arguments in your callback to be. In your case, it's 2;
  • I've set access arguments to be access content which is likely to be enabled for anyone. If this works, then check if you have a access products permission when testing;
  • Remember to clear your cache after modifications. If you use drush, you can use drush cc menu from the command line;
  • Use MENU_CALLBACK because otherwise Drupal will add a menu item to the Navigation menu, since the default is MENU_NORMAL_ITEM.

I've tested this and http://example.org/products/autocomplete.json/cook returns:

"cook"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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