I am using a custom composite field("test") in drupal8 webform in a webform_form_alter() hook. Two select fields-"out_part" and "out_serial" are added to this composite field("test"). "out_serial" is dependent on "out_part" i.e. upon selecting an out_part value from the dropdown, the dropdown values of out_serial are generated. I was able to achieve this with ajax callbacks.

    $form['elements']['test']['#element'] = array(
    'out_part' => array(
            '#type' => 'select',
            '#id' => 'out_part',
            '#options' => array(
            '#ajax' => array(
                'event' => 'change',
                'wrapper' => 'serialNumber_wrapper',
                'callback'=> 'populateOutSerialNumbers',
                'method'=> 'replace'
    'out_serial' => array(
            '#type' => 'select',
            '#id' => 'out_serial',
            '#validated' => TRUE,
            '#attributes' => [
                                'id' => ['serialNumber_wrapper'],

    function populateOutSerialNumbers($form, $form_state){
        $partNumber = $form_state->getValue('test')[1]['out_part'];
        $partNumber = isset($partNumber) ? $partNumber : 'Onboard';

        // Update the $result with dynamic database query results with respect to selected out_part
        $db = Drupal\Core\Database\Database::getConnection();
        $query = $db->query("select distinct T1.serialnumber from assets T1 where T1.dbid <> 0 and (T1.manufacturerpartno = :partNumber)", [':partNumber' => $partNumber]);
        $result = $query->fetchAllKeyed(0,0);

        $form['elements']['test']['#element']['out_serial']['#options'] = $result;

        return $form['elements']['test']['#element']['out_serial'];

This callback works only on the first set of values of the custom composite field. When I click on "Add", the ajax fails to work on the second set of values. Any suggestions on how to overcome this? Do I need to add the ajax to $form['elements']['test']['items'] ?

  • You can't modify $form in an ajax callback. You'll find examples for dependent dropdowns in the Examples module. – 4k4 Aug 31 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.