i have apply button in my drupal node edit form , i am using the form alter hook to add that button , when ever the user clicks on that button i am making an ajax callback to replace the text field value , it is working fine for the first time , but the second time it is not replacing 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 ;
return $form['field_text_plain'];
}
I see the after the first click the form_text_plain id changes to some random value like edit-field-text-plain-0-value-xhfjwos , it looks like that is the issue , is there any way we can stop it from changing ?