В Drupal 8 я хочу сгенерировать опции второго блока выбора на основе вызова Ajax первого блока выбора. В результате генерируется третье новое поле выбора, но мне не нужно создавать новое. Я хочу заменить опции второго окна выбора. Пожалуйста, смотрите мои коды ниже:
public function buildForm(array $form, FormStateInterface $form_state) {
$form['example_select'] = [
'#type' => 'select',
'#title' => $this->t('Select element'),
'wrapper' => 'first',
'#options' => [
'1' => $this->t('One'),
'2' => $this->t('Two'),
'3' => $this->t('Three'),
'4' => $this->t('From New York to Ger-ma-ny!'),
],
'#ajax' => [
'callback' => '::myAjaxCallback',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'edit-output',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Verifying entry...'),
],
]
];
$form['example_select2'] = [
'#type' => 'select',
'#title' => $this->t('Select element'),
'#prefix' => '<div id="first">',
'#suffix' => '</div>',
'#options' => [
],
'#ajax' => [
'callback' => '::myAjaxCallback2',
'disable-refocus' => FALSE,
'event' => 'change',
'wrapper' => 'edit-output',
'progress' => [
'type' => 'throbber',
'message' => $this->t('Verifying entry...'),
],
]
];
return $form;
}
public function myAjaxCallback(array &$form, FormStateInterface $form_state) {
if ($selectedValue = $form_state->getValue('example_select')) {
$arr = array('1' => 'Nice way', '2' => 'Good way');
$form['example_select2']['#options'] = $arr;
}
return $form['example_select2'];
}