2

I have content type with two text fields field1, field2 so i'm using hook_form_alter() to alter my form and i'm added for the first field

  • autocomplete_path path

  • ajax callback and wrraper

and for the second field i'm added

  • prefix : with id ind the wrraper of first field

  • suffix

I want to add a AJAX callback to check the value of a first field and added this value to check a value of second with autocomplete .

The code:

function ajax_callback($form,$form_state) {         
        return $form['field_annonceur']['und']['0']['value'];
    }

    function newsletter_form_alter(&$form, &$form_state, $form_id) {
        //dpm($form['field_annonceur']['und']['0']['value']);
        if ($form_id == 'newsletter_node_form') {
            $form['field_annonceur']['und']['0']['value']['#autocomplete_path'] = 'admin/get_path/autocomplete_field1';
            $form['field_annonceur']['und']['0']['value']['#ajax'] = array(
                'callback' => 'ajax_callback',
                'wrapper' => 'autocomplete_id',
            );

            $form['field_campagne']['und']['0']['value']['#disabled'] = TRUE;
            $form['field_campagne']['und']['0']['value']['#prefix'] = '<div id="autocomplete_id">';
            $form['field_campagne']['und']['0']['value']['#suffix'] = '</div>';

            if (isset($form_state['values']['field_annonceur']['und']['0']['value'])) {
                ['field_campagne']['und']['0']['value']['#disabled'] = FALSE;
                ['field_campagne']['und']['0']['value']['#autocomplete_path'] = "admin/publicite/autocomplete_field2/{$form_state['values']['field_annonceur']['und']['0']['value']}";
            }           
            return $form;
        }
    }

autocomplete_field1($value_field1){};
autocomplete_field2($value_field1,$value_field2){};

but i get the same field when i select a first field , the second field is hidden !!

before input on field 1

enter image description here

after input on field 1

enter image description here

What am I missing? Thanks in advance!

  • ['field_campagne']['und']['0']['value']['#disabled'] - what is this? ['field_campagne']['und']['0']* is the correct way I think. * - ['#disabled'], ['#prefix'] and so on.. But not [n]['value'][*] – xurshid29 Dec 25 '13 at 5:55
  • i'm displayed the form dpm($form) and the [value] works fine the problem in the ajax callback . – El Farouk Mokrani Dec 25 '13 at 8:54
  • I don't thinks that you need add an #ajax attribute to the value of field. Add it to the field itself instead. – xurshid29 Dec 25 '13 at 9:01
  • i'm added the #ajax into the field but no ajax fired :( – El Farouk Mokrani Dec 25 '13 at 9:12
  • drupal.stackexchange.com/questions/31870/… - look at Clive's answer. – xurshid29 Dec 25 '13 at 9:18
1

the solution is to return the target field in the ajax callback so the code is :

function ajax_callback($form,$form_state) {         
        return $form['field_campagne']['und']['0']['value'];
    }

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.