0

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?

4
  • 1
    In Drupal, you cannot add form elements in an Ajax Callback - they will be ignored by the system. They have to be added in the form definition, which is cached BEFORE the Ajax callback. See this tutorial the other day that shows an example of how do to this: jaypan.com/tutorial/…
    – Jaypan
    Jul 9, 2021 at 20:49
  • In my case, I did not add form elements in my ajax callback, I select the right value in an existing form element from a db request, itself from a user request. I carefully red your link (and I tried to adapt my code from it), but it is coded from a programming built of the form. In my case, it is an interactive bulding of the form. So, in your example, I cannot manage the "return $form['search_results'];" in the "ajaxSubmit" function. In fact, I suspect that ajax management can only be done from a programming building of a form. I will test this later. So, thanks for the link.
    – cocq
    Jul 12, 2021 at 7:48
  • I can see you adding an <option> tag in your ajax callback, so you are adding elements in your ajax callback. You can't add anything to the ajax callback, the form is already cached at that point.
    – Jaypan
    Jul 12, 2021 at 15:24
  • Sorry, but I did not add a value, it is a selection of the right value in the select list. But considering that "the form is already cached at that point", it is normal that update the selected element is useless!
    – cocq
    Jul 15, 2021 at 8:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.