9

A menu item is defined with drupal_get_form as callback function and returning the form from call back function. How can I add taxonomy_term_reference field to this form?

$items['files/add'] = array(
      'title' => 'Add file',
      'description' => 'Allows users to add files',
      'type' => MENU_CALLBACK,
      'page callback' => 'drupal_get_form',
      'page arguments' => array('mymodule_add_file'),
      'access callback' => TRUE,
    );
function mymodule_add_file($form, &$form_state) {
    drupal_set_title("Add file");
    $form['mymodule_form'] = array(
      '#type' => 'fieldset',
      '#tree' => TRUE,
      '#collapsable' => FALSE,
      '#title' => 'Adding file to locker room',
    );

    $form['mymodule_form']['file'] = array(
      '#type' => 'managed_file',
      '#title' => 'Upload file',      
    );

    $form['mymodule_form']['tag'] = array(
      '#type' => 'taxonomy_term_reference',
      '#title' => 'Tags',
    );  

    return $form;
}

I am not sure how to add taxonomy_term_reference field for $form['mymodule_form']['tag'] . I want this field to be a text field with auto complete from a vocabulary terms and new term to be added when entered term is not found

5

For Drupal 7, the code is something like this, where field_tags is a taxonomy field in the node with widget type autocomplete.

<?php
   $node=node_load($nid);
    $tags = array();
    foreach ($node->field_tags['und'] as $item) {
      $tags[$item['tid']] = isset($item['taxonomy_term']) ?  $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
    }
    $form['tags'] = array(
      '#type' => 'textfield',
      '#default_value' => taxonomy_implode_tags($tags),
      '#title' => 'Add Tags',
      '#autocomplete_path' => 'taxonomy/autocomplete/field_tags',
      '#maxlength' => 1024,
      '#element_validate' => array('taxonomy_autocomplete_validate')
    );
?>
  • what is page doing at the end of your code? seams to my like invalid php code? – FLY Apr 17 '12 at 8:51
  • That appears to be just a typo. You can safely ignore it, I believe. – BrianV Jul 4 '12 at 19:56
  • With the $form part of the code above, I get the taxonomy field to show up in the form. Autocompletion also works. But how do I store the submitted values in the taxonomy field of a node? $submitted_tags = $form_state['values']['tags']; $node->field_tags[LANGUAGE_NONE][0]['value'] = $submitted_tags; doesn't work for me. Just gives me errors. – deinqwertz Oct 25 '12 at 19:37
0

You need to include the vocabulary id - you should be able to hardcode it so either

$form['mymodule_form']['tag'][$vocabulary->vid] = array(
  '#type' => 'textfield',
  '#default_value' => $typed_string,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid,
  '#required' => $vocabulary->required,
  '#title' => $vocabulary->name,
  '#description' => t('Some description ...").')

);

or for vocab id 5

$form['mymodule_form']['tag']['5'] = array(
  '#type' => 'textfield',
  '#default_value' => $typed_string,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/5',
  '#required' => $vocabulary->required,
  '#title' => $vocabulary->name,
  '#description' => t('Some description ...").')
);

Not tested, but that should work. Else have a gander here: http://drupal.org/node/854216

  • Are you sure this works for D7 as well? I couldnt get it working. It gives exception in taxonomy.module – Srihitha Narra Nov 15 '11 at 9:31
  • hmm, yes it works, but not exactly the way it should. – tecjam Nov 15 '11 at 9:50
  • 1
    This won't work. Drupal 7's taxonomy_autocomplete requires a field name to be passed as an argument. This example uses Drupal 6's taxonomy autocomplete syntax. – BrianV Jul 4 '12 at 19:55
0

I used this and I got autocomplete callbacks working, however not for the specified taxonomy vocab. Instead it returned results from all vocabs

  $element['test'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['test']) ? $items[$delta]['test'] : NULL,
      '#maxlength' => 100,
      '#autocomplete_path' => 'taxonomy/autocomplete/37',
   );

I'm not quite sure why that is to be honest.

  • I tried similarly and below exception is given SELECT t.tid AS tid,t.name AS name FROM {taxonomy_term_data} t WHERE (t.vid IN ()) AND t.name LIKE :db_condition_placeholder_0 ESCAPE &#039;\\&#039;) LIMIT 10 OFFSET 0; Array([:db_condition_placeholder_0]=&gt;%imag%) and mysql is giving exception near &#039;\\&#039;) LIMIT OFFSET 0&#039; – Srihitha Narra Nov 15 '11 at 10:24
0

@tecjam For Drupal 7, you almost had it. All you have to do is use the field name instead of the vocab id.

Like this:

 $element['test'] = array(
 '#type' => 'textfield',
  '#default_value' => isset($items[$delta]['test']) ? $items[$delta]['test'] : NULL,
  '#maxlength' => 100,
  '#autocomplete_path' => 'taxonomy/autocomplete/field_name',
);

Replace field_name with the name of your field.

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.