Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By CatsFromStonehenge on
I have this code in the submitForm() function of a custom form I've created:
$fid = $form_state->getValue('my_form_file_field');
$file = \Drupal\file\Entity\File::load($fid);
Is this right? I can't seem to get the filename of the file chosen by the user (via the form's 'choose file' button).
Thanks!
Comments
Well the syntax is correct.
Well the syntax is correct.
You haven't given us enough information to know what you are trying to do to tell you if your code is correct or not though.
Thanks for feedback :-)
The full code is below. I want to get the filename from the form field of type 'file'. This is where the user can click on a 'Choose File' button in a form, and a file browser dialog appears. Once they choose a file, I want to be able to get the full path and filename, of the file they chose.
Using #file as the type will
Using #file as the type will not handle the saving of the file for you, neither will there be an FID. It only handles the upload to the server.
You should use the #managed_file type, and that will save the file to the files directory, as well as create a database entry with an FID. Then you can use this:
Thank you!
Thanks. The final code was: