2

My question is about textfield autocomplete. I have this code:

function my_module_menu(){
  $items['my_module/textfield_autocomplete'] = array(
    'page callback' => 'my_module_textfield_autocomplete',
    'file' => '.\my_module\my_module.inc',
    'type' => MENU_CALLBACK,
    'access arguments' => array('access example autocomplete'),
  );
  return $items;
}

In my_module.module:

$form['fieldset7-wrapper']['prop'][$i] = array(
          '#type' => 'textfield',
          '#title' => t('Nombre de propietario'),
          // The autocomplete path is provided in hook_menu in ajax_example.module.
          '#autocomplete_path' => 'my_module/textfield_autocomplete'
        );

In my_module.inc:

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

  // Some fantasy DB table which holds clientes

  foreach ($return as $row) {
    $value = check_plain($row->name).' '.check_plain($row->surname1).' '.check_plain($row->surname2);
    //$value = $row->name.' '.$row->surname1.' '.$row->surname2;
    $matches[$value] = $value;
  }

  // return for JS
  drupal_json_output($matches);
}

This works perfect. But I want that json return callback {nid; name} instead of {name,name}, and also show the name in textfield, but I can save nid in my form.

If I change to this my function ajax

$matches[$row->nid] = $value;

The previous sentence returns {nid, name}, but if I choose one value of the list name, in the textfield is shown the nid value, not the name value.

Any suggestions?

2
  • 1
    I don't have time to flesh out a proper answer but you're looking for the #value_callback FAPI property - take a look at the References module, specifically node reference, at these functions: node_reference_field_widget_form() + node_reference_autocomplete_value(). That should make it a bit clearer
    – Clive
    May 10, 2013 at 13:53
  • Thanks! I'll try! Thanks again! Don't worry your help is apreciated!!
    – Royoruiz
    May 10, 2013 at 14:42

1 Answer 1

0

Take a look at the examples module's ajax_example_autocomplete.inc file. You'll need to add a validation function that looks up the nid from the node->title:

  $result = preg_match('/\[([0-9]+)\]$/', $title, $matches);
  if ($result > 0) {
    // If $result is nonzero, we found a match and can use it as the index into
    // $matches.
    $nid = $matches[$result];
    // Verify that it's a valid nid.
    $node = node_load($nid);
    if (empty($node)) {
      form_error($form['node'], t('Sorry, no node with nid %nid can be found', array('%nid' => $nid)));
      return;
    }
  }
  // BUT: Not everybody will have javascript turned on, or they might hit ESC
  // and not use the autocomplete values offered. In that case, we can attempt
  // to come up with a useful value. This is not absolutely necessary, and we
  // *could* just emit a form_error() as below.
  else {
    $nid = db_select('node')
      ->fields('node', array('nid'))
      ->condition('title', db_like($title) . '%', 'LIKE')
      ->range(0, 1)
      ->execute()
      ->fetchField();
  }

Your Answer

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

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