1

It's possible to add exists taxonomy reference field to new custom form?

For example: i have a term field named "field_tags" and would like to add to new form ->

  $form['node_taxonomy_term'] = // load here exists taxonomy autocomplete field "field_tags"
  $form['#submit'][] = 'save_node_terms';
  return $form;

1 Answer 1

3

Not exactly i think, but you can implement next:

$form['node_taxonomy_term'] = array(
  '#type' => 'textfield',
  '#autocomplete_path' => 'ntt/autocomplete',
);

add menu callback for autocomplete path

function HOOK_menu() {
  $items = array();

  $items['ntt/autocomplete'] = array(
    'page callback' => 'node_taxonomy_term_autocomplete',
    'page arguments' => array(2),
    'type' => MENU_CALLBACK,
  ); 
  return $items;
}

and add callback function

function node_taxonomy_term_autocomplete($str = 0) {
  $matches = array();
  //vocabulary id
  $vid = 2;

  $result = db_select('taxonomy_term_data', 't')
    -> fields('t', array('tid', 'name'))
    -> condition('vid', $vid, '=')
    -> condition('name', $str.'%%', 'LIKE')
    -> range(0, 10)
    -> execute();

  foreach ($result as $term) {
    $matches[$term -> tid] = check_plain($term -> name);
  }

  drupal_json_output($matches);
}

enjoy

2

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.