0

I'm having troubles with a FORM in Drupal 8, the idea is:

Chose a radio value from radio-estaciones to recharge radio-contaminantes, but, after the callback, the result I see is empty, but in replace command I use, I see it correcly

This is the code of buildForm

public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state){

    $arrRadioContaminantes = array();
    $arrRadioContaminantes["0"] = t("ALL");

    //My function to access DB
    $result = \Drupal\map_data\Controller\Contaminante::list();
    foreach ($result as $it){
        $arrRadioContaminantes[$it["id_contaminante"]] = t($it["description"]);
    }

    $form['radio_contaminantes'] = array(
        '#type' => 'radios',
        '#options' => $arrRadioContaminantes,
        '#ajax' => [
            'callback' => '::seleccionContaminantes',
            'disable-refocus' => true,
            'event' => 'change',
            'wrapper' => 'radios-estaciones',
            'method' => 'replace'
        ]
    );

    $form['radio_estaciones'] = array(
        '#type' => 'radios',
        '#id' => 'radios-estaciones',
        '#options' => ['-1'=>t("SELECT CONTAMINANTE FIRST")]
    );

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => $this->t('Submit'),
        '#button_type' => 'primary',
    );

    $form_state->setCached(false);
    $form_state->setRebuild(true);

    return $form;
}

This is callback function:

public function seleccionContaminantes(array &$form, FormStateInterface $form_state){
    if($form_state->getValue("radio_contaminantes") != 0){
        $form['radio_estaciones']["0"] = t("All");
    }

    //My Database function
    $resultado = \Drupal\montar_mapa\Controller\Estacion::list($form_state->getValue("radio_contaminantes"));
    foreach ($resultado as $it){
        $form['radio_estaciones'][$it["id"]] = t($it["name"]);
    }

    $ajaxResp = new AjaxResponse();
    $ajaxResp->addCommand(new \Drupal\Core\Ajax\ReplaceCommand('radios-estaciones', \Drupal::service('renderer')->render($form['radio_estaciones'])));

    return $ajaxResp;
}

I try returning $form['radio_estaciones'] with same results, and try DataCommand pasing new values, same result... I need help

10
  • Just return $form['radio_estaciones']; – No Sssweat Nov 25 '19 at 14:42
  • I tried too, the response I have is empty, I only see twigs annotations, nothing else in both cases. data: <!-- THEME DEBUG --> <!-- THEME HOOK: 'radios' --> <!-- BEGIN OUTPUT from 'core/modules/system/templates/radios.html.twig' --> <div id="radios-estaciones"></div> <!-- END OUTPUT from 'core/modules/system/templates/radios.html.twig' --> I don't know if twig hook is doing something – Nacho Escursell Nov 25 '19 at 14:44
  • If you do, return 'hello world'; does it work? – No Sssweat Nov 25 '19 at 14:47
  • No @NoSssweat in fact, if I use debuger, renderedContent() don't reflect changes either. Thats the answer I see: data: <!-- THEME DEBUG --> <!-- THEME HOOK: 'radios' --> <!-- BEGIN OUTPUT from 'core/modules/system/templates/radios.html.twig' --> <div id="radios-estaciones"></div> <!-- END OUTPUT from 'core/modules/system/templates/radios.html.twig' --> – Nacho Escursell Nov 25 '19 at 14:50
  • If you switch to the default "Bartik" theme, does the same issue occur? Also try turning off twig debug. – No Sssweat Nov 25 '19 at 14:53
0

After change type to 'select' works fine.

Drupal have this bug active

https://www.drupal.org/project/drupal/issues/2758631

Same situation with checkboxes

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.