I'm trying to set up a function to pull terms from a referenced node and pre-fill the fields on a new node. I can get everything working right up to the point where I need to insert the collected values back in and I just can't figure out how to do it.

To explain my scenario better, I have a Node Reference field. When a user enters a value in there and presses the pre-fill button I created, a function fires to collect the terms that have been saved on the referenced node/s.

All the fields are Taxonomy terms and use the Autocomplete deluxe widget.

I tried using the default http://api.drupal.org/api/drupal/includes%21form.inc/function/form_set_v... but just couldn't get it to play. Should it work?

So how, from template.php or a custom module, do you programatically add new terms to autocomplete deluxe fields?

CommentFileSizeAuthor
#3 screenshot.png3.75 KBfishfree
#1 setting values.png15.43 KBLNakamura
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

LNakamura’s picture

Issue summary: View changes
Status: Active » Postponed (maintainer needs more info)
FileSize
15.43 KB

Hi, @marblegravy - have you figured this out yet? If not, and assuming you're currently using the 2.x branch of Autocomplete Deluxe (1.x is no longer supported), here's what I did in a hook_form_alter() to programmatically set the values in an Autocomplete Deluxe widget:

$form_state['input']['YOUR_FIELD_MACHINE_NAME_tid']['value_field'] = '""Tag 1"" ""Tag 2"" ""Tag 3""';

Let us know - thanks!

Note: in my case, I was modifying a Views exposed filter, hence the "_tid" at the end of the field name...

fishfree’s picture

@LNakamura Could you tell me how to do it in a form element ajax callback?


function zwznertags_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'book_node_form') {
    //sdpm($form);
    $form['field_persons']['zwznertags_persons'] = array(
      '#type' => 'button',
      '#value' => 'Recognize person names',
      '#limit_validation_errors' => array(),
      '#ajax' => array(
        'callback' => 'zwznertags_persons_callback',
        'wrapper' => 'edit-field-persons',
       )
    );
  }
}

function zwznertags_persons_callback(&$form, &$form_state){
  watchdog('zwznertags', 'test');// Succeeded
  //dpm($form);
  $form_state['input']['field_persons']['und']['value_field'] = '""Mike"" ""Bill"" ""Smith""'; // Your method does not work here.
}

fishfree’s picture

FileSize
3.75 KB

My screenshot: screenshot