Enable to override options array of checkboxes in view better exposed filter form

Created on 30 November 2022, almost 2 years ago
Updated 11 April 2024, 8 months ago

In one of my view I am added exposed filter option in view exposed filter using better exposed filter module.
I have added country code field from address module in exposed filter & added one ajax call in it.
On this ajax call we want to update city in 2nd dropdown based on country select.

Below are my code for Ajax call implementation in .module file in hook_form_alter

if (isset($form['field_job_location_details_country_code'])) {
            $form['field_job_location_details_country_code']['#ajax'] = [
                'callback' => 'myAjaxCallback',
                'disable-refocus' => true,
                'event' => 'change',
                'wrapper' => 'city-checkboxes',
                'method' => 'replace',
                'disable-refocus' => FALSE,
                'progress' => array(
                    'type' => 'throbber',
                    'message' => t('Searching cities...'),
                ),
            ];
}
/**
 * Ajax callback.
 */
function myAjaxCallback(array $form, FormStateInterface $form_state) {
    //echo "Test are there.........";
    
    $form_state->setRebuild(TRUE);
    
    
    $localities = [1 => "Slough", 2 => "London", 3 => "Liverpool"];
    $form['field_job_location_details_locality']['#options'] = [1 => "Slough", 2 => "London", 3 => "Liverpool"];
    

    Checkboxes::processCheckboxes($form['field_job_location_details_locality'],$form_state,$form);
    $response = new AjaxResponse();

    $renderer = \Drupal::service('renderer');
    foreach (Element::children($form['field_job_location_details_locality']) as $i) {
        unset($form['my_field'][$i]);
    }
    \Drupal::formBuilder()->doBuildForm($form['#form_id'], $field, $form_state);
    $response->addCommand(new ReplaceCommand("#my_field_id", $renderer->render($form['field_job_location_details_locality'])));

    return $response;  
}

This code is not working for me, ajax call is happened but this option is not chnaged.

Can anyone please let me know is there any solution to update checkboxes option on ajax callback using form_alter?

๐Ÿ› Bug report
Status

Closed: outdated

Version

5.1

Component

Code

Created by

๐Ÿ‡ฎ๐Ÿ‡ณIndia NitinSP

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    This doesnโ€™t appear to be a bug related to this module. Would recommend trying a Drupal forum or stack overflow for assistance with your custom Ajax

Production build 0.71.5 2024