Yay!!! Now all I needed was apply the same checks that file_save_upload does to prevent most hacks. I preferred a more civilised (read Drupal’ised □ ) way though, and ultimately thought why not check the existence of the file upload in Symfony’s files property on the Request object.Īnd voila, I found it there with the name as passed by the client. I had atleast some way to get the work done. I found the file upload in PHP‘s $_FILES superglobal which was good. Ultimately all this is HTTP and good old multipart form submissions and there should be a way to get our hands on the file upload from the client-side. And the file_save_upload function can only handle those files which are uploaded via either file or managed_file form elements from Drupal’s FAPI, as those form elements appropriately assign and update the Request class’ files property as expected by file_save_upload function.Ĭ’mon this shouldn’t be that difficult was what I told myself at this point. On a closer look at the function’s code and other areas of Drupal, it appeared Drupal uses own internal logic to assign file names in the files collection of Symfony’s Request object. The function did not recognise the name and returned NULL. I tried using the same function to save files from our dynamically added file form fields in javascript passing the name attribute of the form field to the file_save_upload function. FileTestForm uses the file_save_upload function to save file server-side. I tried to look at the FileTestForm class which uses the file field. The next logical destination was to look at the file form field from the FAPI. And you would get a File class instance in your submitForm handler when you call $form_state->getValue('file_field_name') in the handler (well actually an array of File instances). ManagedFile form field would automatically upload the file to Drupal in an Ajax’ed form submission as soon as a file is selected. Which completely disassociates the responsibility of handling file uploads from your Drupal forms. Plus I thought the functionality might come-in handy in future also when for example, providing drop-areas for files (for drag-drop file upload) which doesn’t use the traditional form fields.Īlmost all file-upload functionality we create today for our Drupal-based products uses the managed_file form fields. But we had a legacy form, a lot of which was generated client-side in javascript and submitted as json to the server.Īnd my focus was on getting the job done quickly (of adding the new file field support) to the form rather than re-writing the entire client-side functionality to use Drupal’s Ajax’ed forms. And I would agree to the suggestion for most use-cases. I know almost all of Drupal’ers would point towards using Drupal’s ajax-based Form API (FAPI) to inject the new file fields on the form. While working with Mart, we had a situation recently where we needed to add file ( ) form fields dynamically in javascript client-side. I hope all of you and your loved ones are keeping safe. What, almost after 6 months I guess!! We came out from one lockdown, fumbled the freedom around to go into another and now coming out of the second all during this time.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |