In a form I want to bulk upload jpgs renaming them using exif date information and dynamically setting the #upload_location according to the value of a textfield in the same form.
I have no problem with accessing the exif date information. What I don't know how to do, is dynamically set the file name and file path (actual location on the server) programmatically.
This is my form upload element:
$form['images'] = array( '#type' => 'managed_file', '#multiple' => TRUE, '#upload_validators' => array( 'file_validate_extensions' => array('jpg'), ), '#upload_location' => 'public://photos', '#required' => TRUE, ); $form['file'] = [ '#type' => 'fieldset', '#title' => $this->t('File'), ]; $form['file']['name'] = [ '#type' => 'textfield', '#title' => $this->t('File name'), '#description' => $this->t( 'Is suffixed by the exif creation date-time plus fid cleaned for urls. If not set the original file name will be used.' ), ]; $form['file']['path'] = [ '#type' => 'textfield', '#title' => $this->t('File path'), '#placeholder' => 'path/to/directory', '#description' => $this->t( 'Path to the files sub-directory, default is "photos". Sub-dirs will be create if necessary.' ), ]; I've tried using something like this in both validateForm() and submitForm().
$images = $form_state->getValue('images'); $file = File::load( $images[0] ); $file->setFileName("xxx2"); $file->setFileUri("public://photos/test/xxx2.jpg"); Although this changes the values of the $file object, it doesn't change the real path. The jpg files are also still in /sites/default/files/photos/ with their original name plus a unique counter.
So to repeat my question: How can I set the upload_location and the fileName of a managedFile dynamically and programmatically in a submitForm() or validateForm() function using methods that are not marked deprecated in drupal 8.
file_move