Scenario:
I have a custom form whose first step is to upload a file using a managed_file field element.
Then, after this manged file is uploaded I want to rebuild the form using information from the managed file and display new form elements that should only exist after the managed file is uploaded.
I have struggled with this for days and cannot get a working solution. The first problem is that the managed_file already has an ajax callback so I can't seem to add my own to populate and/or return field elements after the file is successfully uploaded.
I've gone as far as overriding the managed_file type with a custom form field type but I can't seem to be able to return actual form elements AND an ajax response (one of the other). Code example of my override below.
In short, how can I repopulate a custom field after a managed file uploaded is completed and add new fields dependent on the file that was uploaded?
/**
* My Managed File Element.
*
* @FormElement("my_managed_file")
*/
class MyManagedFile extends ManagedFile {
/**
* {@inheritdoc}
*/
public static function uploadAjaxCallback(&$form, FormStateInterface &$form_state, Request $request) {
$response = parent::uploadAjaxCallback($form, $form_state, $request);
return $response;
}
}
$response->addCommand(new ReplaceCommand(..., ...));
before returning?