DRUPAL 8-Ajax回调后无法更新单选按钮字段

时间:2019-11-25 11:58:12

标签: php ajax drupal drupal-8

我在Drupal 8中遇到了FORM的麻烦,想法是:

从无线电台中选择一个无线电值来给无线电台充电,但是,在回调之后,我看到的结果为空,但是在我使用的replace命令中,我看到的是正确的

这是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;
}

这是回调函数:

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;
}

我尝试返回具有相同结果的$ form ['radio_estaciones'],然后尝试DataCommand粘贴新值,相同结果...我需要帮助

1 个答案:

答案 0 :(得分:0)

将类型更改为“选择”后,效果很好。

Drupal的bug处于活动状态

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

情况与复选框相同