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, DrupalCoreFormFormStateInterface $form_state){ $arrRadioContaminantes = array(); $arrRadioContaminantes["0"] = t("ALL"); //My function to access DB $result = Drupalmap_dataControllerContaminante::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 = Drupalmontar_mapaControllerEstacion::list($form_state->getValue("radio_contaminantes")); foreach ($resultado as $it){ $form['radio_estaciones'][$it["id"]] = t($it["name"]); } $ajaxResp = new AjaxResponse(); $ajaxResp->addCommand(new DrupalCoreAjaxReplaceCommand('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