creating a custom submit function for the form of mymodule

 * Implements hook_form_submit()
function mymodule_form_submit($form, &$form_state){

    $new_user = drupal_anonymous_user();
    // removed basic user fields here to make it more clear

    // save taxonomy checkbox field
    foreach ($form_state['values']['third']['field_functioncat'] as $key => $value) {
        if($value > 0){
            $new_user->field_functioncat[LANGUAGE_NONE][$key]['tid'] = $value;

    // save text field
    $new_user->field_firstname[LANGUAGE_NONE][0]['value'] = $form_state['values']['first']['field_firstname'];


Above code works but I'm having trouble saving the autocomplete field.

What I'm trying:

// get value of the form
$tid = taxonomy_get_term_by_name($form_state['values']['first']['field_city'], 'field_city');
// get the first array value and then the tid of the left over object:
$tid = reset($tid)->tid;
// save field to user profile
$new_user->field_city[LANGUAGE_NONE][$tid]['tid'] = $tid;

This works for existing terms, but when I put in a new term it can't get the id of the term because that term name is not created?

How do I create the term name and get the tid to submit it to the user creation?

note: I've created the field_city as taxonomy term and linked it to the user profile so it's visible and editable in on the user edit profile page.

1 Answer 1


As you said if you're unable to find the tid then its a new term. You can save the new term using taxonomy_term_save()

For example,

$term = new stdClass();
$term->vid = <field city vid>;
$term->name = $form_state['values']['first']['field_city'];

After saving the new term you can access the tid like this,


If you want to fetch the vid referenced with particular field then look at the following thread - How to get the field settings in drupal 7

  • thats looks like a good solution. I'll try that! +1
    – FLY
    Commented Sep 5, 2012 at 12:33
  • It kinda works :) I had to get the first value of the $form_state['values']['first']['field_city'] array otherwise blank values were inserted into the vocabulary but got it working now! THNX!!!
    – FLY
    Commented Sep 5, 2012 at 12: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.