I add a ajax callback to a select field
$form['field_equipe_membre']['widget']['#ajax'] = array(
'callback' => 'ajax_desactive_unites_field_callback',
'event' => 'change',
'method' => 'html',
'wrapper' => 'edit-field-unite-de-recherche-wrapper',
);
From the triggering_element field, I succeeded to retrieve a value via a db request and select it in another select field
$trigger = $form_state->getTriggeringElement();
$selected_equipe = $trigger['#value'];
$query....
$response = new AjaxResponse();
$content = '<option value=\"'.$row_unite['nid'].'\" selected=\"selected\">'.$row_unite['title'].'</option>';
$response->addCommand( new AppendCommand('#edit-field-unite-de-recherche', $content));
return $response;
But when I submit the form, the chosen value by the callback is not saved.
What is the magic lines or the right method to do this?
I tested
$form_state->setRebuild();
in the callback ; I also tested
$form_state->setValue('field_unite_de_recherche', $row_unite['nid']);
$form_state->setCached(TRUE);
I tested the solution proposed somewhere in this forum to define a hidden field in hook_form_edit_alter
$form['product_id']['#type']='hidden';
$form['product_id']['#prefix']='<div id="liberte_pid">';
$form['product_id']['#suffix']='</div>';
and save the value in the callback
$form['product_id']['#value'] = array(
$row_unite['nid'],
);
But none of these solutions gave the solution for me.
I also red the https://www.drupal.org/project/drupal/issues/2263569 page, but I do not know what to do with.
So : how to save this chosen value by a ajax callback?