1

I am about to create a filter views handler extended from views_handler_filter_string (you can see the class under views/handlers), so I have to override my method value_form to create a new value form form my filter handler.

function value_form(&$form, &$form_state) {
    $countries = _prepare_countries_list();
    $selected_country = isset($form_state['values']['value']['country'])?$form_state['values']['value']['country']:key($countries);

    $values = array(
        'country' => array(
            '#type'    => 'select',
            '#title'   => t('Country'),
            '#options' =>$countries,
            '#ajax'    => array(
                'callback' => 'country_state_dependent_dropdown_callback',
                'wrapper'  => 'dropdown-states',
            ),
        ),
        'state' => array(
            '#type'    => 'select',
            '#title'   => t('State'),
            '#prefix' => '<div id="dropdown-states">',
            '#suffix' => '</div>',
            '#options' => _country_state_get_second_dropdown_options($selected_country),
        ),
    );
    $form['value'] = $values;
    return $form;
}

I am trying here to set an ajax callback for my drop down list select. Usually we implement our callback function and we return $form['element'], but in my case it gives no result.

Anyone who has tried to override the value_form before to add some AJAX able to help me resolve this issue?

  • Please correct me if I'm wrong. You are trying to update the options in your second select element , every-time your first select element changes it's value? – alyssaeliyah Apr 17 '15 at 0:31
  • have you answered already your question?Why did you post your supposed "reply" in the answer section? :) – alyssaeliyah Apr 17 '15 at 0:43
  • iam sorry, i didnt answer it yet. i was mistake – blel marouane Apr 17 '15 at 0:54
  • yes you are right. so with drupal form i can handle it but in this case when overrding value_form(&$form, &$form_state) method of my views/handlers/views_handler_filter_string.inc.php class it's complicated – blel marouane Apr 17 '15 at 0:55
1

This is untested but at least, you get the logic. I'm sorry if I edited your coding style convention.

function value_form(&$form, &$form_state)
{
    $form = array();

    $countries = _prepare_countries_list();
    $selected_country = isset($form_state['values']['country'])?$form_state['values']['country']:key($countries);

    // Country dropdown list
    $form['country'] = array(
        '#title' => t('Country'),
        '#type' => 'select',
        '#options' => $countries,
        '#ajax' => array(
            'event'=>'change',
            'callback' =>'country_state_dependent_dropdown_callback',
            'wrapper' => 'dropdown-states',
        ),
    ); 

    // Wrapper for state dropdown list
    $form['state_wrapper'] = array(
        '#prefix' => '<div id="dropdown-states">',
        '#suffix' => '</div>',
    ); 


    // State dropdown list
    $form['state_wrapper']['state'] = array(
        '#title' => t('State'),
        '#type' => 'select',
        '#options' => _country_state_get_second_dropdown_options($selected_country),
    ); 

    return $form;
}


/**
* Function for handling ajax request
*/
function country_state_dependent_dropdown_callback($form, $form_state){
    return $form['state_wrapper'];
}
| improve this answer | |

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.