4

I have a field organizations on the user profile(the field can accept multiple values). I would like to add a autocomplete to this field. My code for this is

<?php
/**
 * Implementation of hook_menu()
 */
function org_ac_menu() {
  $items = array();
  $items['org_ac/autocomplete'] = array(
    'page callback' => 'org_ac_autocomplete',
    'access callback' => TRUE,
    'access arguments' => array('access dbpedia autocomplete'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_form_alter()
 */
function org_ac_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    //$form['field_organization']['#autocomplete_path'] = 'org_ac/autocomplete';
    $form['field_organization']['und'][0]['value']['#autocomplete_path'] = 'org_ac/autocomplete'; **/* This works but only on the first input field! */**
  }
}

/**
 * Check for possibilities in dbpedia
 */
function org_ac_autocomplete($string = '') {
  $found = array();

  $url = 'http://lookup.dbpedia.org/api/search.asmx/PrefixSearch?QueryString='.urlencode($string).'&MaxHits=5';
  $result = file_get_contents($url);
  $data = simplexml_load_string($result);

  foreach($data->Result as $res_temp)
  {
      $found[check_plain($res_temp->Label)] = check_plain($res_temp->Label);
  }

  drupal_json_output($found);
}

The code works when I reference this element on the form, ->

$form['field_organization']['und'][0]['value']['#autocomplete_path'] = 'org_ac/autocomplete'; 

But since it can accept multiple values, the autocomplete doesn't work on the remaining elements ->$form['field_organization']['und'][1] & $form['field_organization']['und'][2]['value']['value']

How can I make it work on all the input elements for the organization field ?

Thanks

6
  • It works when i change the form element to $form['field_organization']['und'][0]['value']['#autocomplete_path'] = 'org_ac/autocomplete'; Is there a way to have an auto complete for every field of Organization ?
    – Jayaram
    Commented Dec 12, 2012 at 20:51
  • Can't you just lose the check on form_id then?
    – devunder
    Commented Dec 12, 2012 at 21:00
  • That check is to ensure that its only on user profile.
    – Jayaram
    Commented Dec 12, 2012 at 21:03
  • I understand, what do you mean by: "Is there a way to have an auto complete for every field of Organization?" then?
    – devunder
    Commented Dec 12, 2012 at 21:14
  • My auto complete field can accept unlimited values. So there is always a possibility to add another value to the organizations field. My question is how do i set the auto complete to all the inputs fields of organization ? When I use $form['field_organization']['und'][0]['value'], it only sets auto complete to the first input field. what about the other indices ?
    – Jayaram
    Commented Dec 12, 2012 at 21:38

1 Answer 1

2

If you modify your hook_form_alter to loop through the $delta of field_organization it should work for you on multiples...

/**
 * Implementation of hook_form_alter()
 */
function org_ac_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_profile_form') {
    foreach($form['field_organization'][LANGUAGE_NONE] as $delta => $field) {
      if(is_array($field) && is_numeric($delta)) {
        $form['field_organization'][LANGUAGE_NONE][$delta]['value']['#autocomplete_path'] = 'org_ac/autocomplete';
      }
    }
  }
}
3
  • 2
    this will work in almost every case (+1). Chuck Norris would add another foreach() to workaround the language (und) as well :)
    – AKS
    Commented Feb 26, 2014 at 18:29
  • 1
    @ayesh-kLANGUAGE_NONE would be more appropriate. $form['field_organization'][LANGUAGE_NONE][$delta]
    – JudaPriest
    Commented Sep 17, 2014 at 15:05
  • JudaPriest and Ayesh K are correct. Should be using LANGUAGE_NONE or use $lang = $form_state['values']['language']; to grab language if multi-lingual and use $lang in place of LANGUAGE_NONE above. Updated. Thanks!
    – Brent
    Commented Sep 17, 2014 at 19:47

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.