I have an ajax callback function that needs to return a form element and also an ajax response. Is it possible to do both?
In my callback I need to set and return something like:
$form['fid'] = array(
'#type' => 'textfield',
'#value' => 1,
);
return $form['fid'];
And also something like:
$response = parent::uploadAjaxCallback($form, $form_state, $request);
return $response;
Is there a way that I can achieve return both a form element and an ajax response?
EDIT: Latest attempt is returning the form element for display but when I examine the $form from the form build function the value is still empty.
class MyManagedFile extends ManagedFile {
/**
* {@inheritdoc}
*/
public static function uploadAjaxCallback(&$form, FormStateInterface &$form_state, Request $request) {
$response = parent::uploadAjaxCallback($form, $form_state, $request);
$form['fid'] = array(
'#type' => 'textfield',
'#prefix' => '<div id="fid">',
'#suffix' => '</div>',
'#value' => '1',
);
$response->addCommand(new ReplaceCommand('#fid', $form['fid']));
return $response;
}
}
Examining the $form from the form builder (dpm($form)) results in:
[fid] => Array ( [#type] => textfield [#prefix] =>
[#suffix] =>
)