Score:0

Add a text field value with AJAX

cn flag

Problem: After an AJAX callback, the text is not inserted in the body field.

Description: I created a fresh Drupal installation. Added a field 'Type' | field_type | List (text) to the Article CT. The goal was that a change in that select would add text to the body field. Here's the ajax_test.module code, inspired by the documentation:


use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_form_alter()
 */
function ajax_test_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id === 'node_article_form') {

    $form['body']['#prefix'] = '<div id="edit-body-wrapper">';
    $form['body']['#suffix'] = '</div>';

    $form['field_type']['widget']['#ajax'] = [
      'callback' => 'myAjaxCallback',
      'disable-refocus' => FALSE,
      'event' => 'change',
      'wrapper' => 'edit-body-wrapper',
      'progress' => [
          'type' => 'throbber',
          'message' => t('Updating'),
      ],
    ];
  }
  // Outside the AJAX callback, the default_value works
  // $form["body"]["widget"][0]["#default_value"] = 'test';
}


function myAjaxCallback(array &$form, FormStateInterface $form_state) {
  if ($selectedValue = $form_state->getValue('field_type')) {
    $form["body"]["widget"][0]["#default_value"] = 'test';
    // Here it won't
  }
  return $form['body'];
}

If I set the default_value without the AJAX, it appears correctly. Using Xdebug, I can see that the callback is updating the value correctly, but nothing appears in the body on completion.

Any suggestions would be very appreciated.

rfmarcelino avatar
cn flag
I forgot to copy a part of the code that sets the wrapper id. Since I can't edit it, here it is: $form['body']['#prefix'] = '<div id="edit-body-wrapper">'; $form['body']['#suffix'] = '</div>';
id flag
The original author of a question or answer may always edit their own post, regardless of reputation level.
rfmarcelino avatar
cn flag
Thank you. Somehow I missed it before. Edited.
Score:0
cn flag

I finally found out a solution here: AJAX form - replacing #default_value not working (the '2021 edit...') In this specific case, the callback would work:

function myAjaxCallback(array &$form, FormStateInterface $form_state) {
if ($selectedValue = $form_state->getValue('field_type')) {
    $userInput = $form_state->getUserInput();
    unset($userInput["body"]);
    $form_state->setUserInput($userInput);
    $form["body"]["widget"][0]["#default_value"] = 'test';
  }
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.