I am using hook_form_alter()
hook to add an Apply button to the node edit form.. When users click on that button, an AJAX callback replaces the text field value.
It works fine the first time, but the second time it doesn't replace the value.
function course_form_node_items_edit_form_alter(&$form, FormStateInterface $form_state) {
$form['apply'] = array(
'#type' => 'submit',
'#value' => t('Apply'),
'#weight' => '38',
'#executes_submit_callback' => FALSE,
'#limit_validation_errors' => array(),
'#ajax' => array(
'callback' => 'button_test_callback',
'wrapper' => 'edit-field-text-plain-0-value',
),
);
}
function button_test_callback(&$form, FormStateInterface $form_state) {
$item_id = $form['field_item_id']['widget'][0]['value']['#value'];
$sql = "SELECT item_name
FROM items
WHERE field_item_id = '" . $item_id . "'";
$location = \Drupal::database()->query($sql)->fetchAssoc();
$form['field_text_plain']['widget'][0]['value']['#value'] = $location;
$form['field_text_plain']['widget']['0']['value']['#id'] = 'edit-field-text-plain-0-value';
return $form['field_text_plain'];
}
After the first click, the form_text_plain ID changes to some random value, for example edit-field-text-plain-0-value-xhfjwos. It seems that is the issue.
Is there any way to avoid the form_text_plain ID changes?