How to change autocomplete_route_parameters value on change of select option?

I have a form element and inside autocomplete_route_parameters, I want to change $langcode value when I select any language from select list.

$form['node_items'] = array(
            '#type' => 'textfield',
            '#autocomplete_route_name' => 'autocomplete_node_search.autocomplete',
            '#autocomplete_route_parameters' => array('skip_node_id_string' => $skip_node_id_string, 'langcode' => $langcode),


So if I choose language french from select list, $langcode should become = 'fr' via ajax.


I have used below example to do that.


Customized it as listed below.

$form_state_values = $form_state->getValues();
if (!empty($form_state_values['language'])) {
  // write your logic here to prepare $skip_node_id_string & $langcode
$form['first'] = [
 '#type' => 'textfield',
 '#title' => $this->t('first'),
 '#autocomplete_route_name' => 'autocomplete_node_search.autocomplete',
 '#autocomplete_route_parameters' => 
 array('skip_node_id_string' =>$skip_node_id_string, 
 'langcode' => $langcode),
 '#prefix' => '<div id="change-lang-wrap">',
 '#suffix' => '</div>',
$form['language'] = [
 '#type' => 'select',
 '#title' => $this->t('language'),
 '#options' => ['someval'],
 '#ajax' => [
  'callback' => [$this, 'changeLanguage'],
  'event' => 'change',
  'wrapper' => 'change-lang-wrap',
// Ajax callback.
public function changeLanguage(array &$form, FormStateInterface $form_state) {
 return $form['first'];

Hope this will fix your issue.

Tried below links, but no luck.


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.