Hello,

I am trying to setup an autopopulating field in Drupal 7. I used this tutorial. Everthing seems to be working but the autopopulate which is a problem.

Using the tutorial the '#autocomplete_path' => 'user/autocomplete', does produce auto completion.

Here is my code

<?php

//PSONumbersField

function PSONumbersField_form_alter(&$form, &$form_state, $form_id) {
	if($form_id=='webform_client_form_9'){
		$form['submitted']['pso_contact_information']['ahrq_assigned_pso_number']=array(
		'#type' => 'textfield',
          '#title' => t('PSO Number'),
          '#size' => 30,
          '#maxlength' => 60,
          '#autocomplete_path' => 'psonumberfield/autocomplete',
          '#weight' => -1,
		  );
	}
}
function PSONumbersField_menu(){
	$items['psonumberfield/autopopulate'] = array(
    'title' => 'PSO Number Autopopulate',
    'page callback' => 'psonumbersfield_autopopulate',
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

    return $items;
	}
function psonumbersfield_autopopulate($string) {
  $matches = array();
  $result = db_select('field_data_pso_number', 'c')
    ->fields('c',array('pso_number_value'))
	->condition('c.pso_number_value', '%' . db_like($string) . '%', 'LIKE')
    ->execute();

  // save the query to matches
  foreach ($result as $row) {
    $matches[$row->pso_number_value] = check_plain($row->pso_number_value);
  }

  // Return the result to the form in json
  drupal_json_output($matches);
  
}

Things I have tried:
1 Clearing the cache
2 Visiting the url: domain.com/psonumberfield/autopopulate is producing an array with the correct data:
{"P0032":"P0032","P0068":"P0068","P0103":"P0103","P0020":"P0020","P0128":"P0128","P0051":"P0051","P0141":"P0141","P0099":"P0099","P0111":"P0111","P0083":"P0083","P0002":"P0002","P0097":"P0097","P0094":"P0094","P0136":"P0136","P0149":"P0149","P0078":"P0078","P0074":"P0074","P0119":"P0119","P0122":"P0122","P0015":"P0015","P0145":"P0145","P0008":"P0008","P0069":"P0069","P0133":"P0133","P0124":"P0124","P0155":"P0155","P0134":"P0134","P0148":"P0148","P0081":"P0081","P0157":"P0157","P0123":"P0123","P0009":"P0009","P0022":"P0022","P0024":"P0024","P0063":"P0063","P0095":"P0095","P0048":"P0048","P0034":"P0034","P0038":"P0038","P0143":"P0143","P0025":"P0025","P0076":"P0076","P0082":"P0082","P0041":"P0041","P0047":"P0047","P0052":"P0052","P0129":"P0129","P0084":"P0084","P0120":"P0120","P0028":"P0028","P0075":"P0075","P0140":"P0140","P0091":"P0091","P0039":"P0039","P0014":"P0014","P0132":"P0132","P0115":"P0115","P0106":"P0106","P0153":"P0153","P0112":"P0112","P0113":"P0113","P0144":"P0144","P0154":"P0154","P0152":"P0152","P0146":"P0146","P0067":"P0067","P0066":"P0066","P0135":"P0135","P0012":"P0012","P0087":"P0087","P0043":"P0043","P0089":"P0089","P0079":"P0079","P0007":"P0007","P0110":"P0110","P0096":"P0096","P0118":"P0118","P0151":"P0151","P0030":"P0030","P0017":"P0017","P0031":"P0031","P0035":"P0035","P0058":"P0058","P0065":"P0065","P0027":"P0027","P0090":"P0090","P0102":"P0102","P0029":"P0029","P0049":"P0049","P0062":"P0062","P0001":"P0001","P0072":"P0072","P0050":"P0050","P0045":"P0045","P0010":"P0010","P0023":"P0023","P0077":"P0077","P0003":"P0003","P0071":"P0071","P0114":"P0114","P0117":"P0117","P0033":"P0033","P0086":"P0086","P0026":"P0026","P0036":"P0036","P0126":"P0126","P0021":"P0021","P0121":"P0121","P0018":"P0018","P0013":"P0013","P0059":"P0059","P0098":"P0098","P0006":"P0006","P0054":"P0054","P0046":"P0046","P0037":"P0037","P0040":"P0040","P0019":"P0019","P0053":"P0053","P0105":"P0105","P0005":"P0005","P0056":"P0056","P0061":"P0061","P0070":"P0070","P0057":"P0057","P0073":"P0073","P0016":"P0016","P0088":"P0088","P0042":"P0042","P0104":"P0104","P0080":"P0080","P0060":"P0060","P0044":"P0044","P0064":"P0064","P0161":"P0161","P0159":"P0159","P0162":"P0162","P0164":"P0164","P0163":"P0163","P0011":"P0011","P0165":"P0165","P0168":"P0168","P0169":"P0169","P0166":"P0166","P0176":"P0176","P0175":"P0175","P0174":"P0174","P0170":"P0170","P0178":"P0178","P0180":"P0180","P0182":"P0182","P0186":"P0186","P0187":"P0187","P0189":"P0189","P0188":"P0188","P0184":"P0184","P0185":"P0185","P0191":"P0191","P0192":"P0192"}

3 When I filter after the last slash, it is working. For example domain/psonumberfield/autopopulate/P002 produces
{"P0020":"P0020","P0022":"P0022","P0024":"P0024","P0025":"P0025","P0028":"P0028","P0027":"P0027","P0029":"P0029","P0023":"P0023","P0026":"P0026","P0021":"P0021"}

It seems like I am missing one small thing to get this to work. Any Ideas?

Comments

thetailwind’s picture

Sorry, I am an idiot. My autocomplete_path did not match the hook_menu Items array.
psonumberfield/autocomplete != psonumbersfield_autopopulate