1

I am trying to create a field to upload the xml format of site map. But when I add a file to my filed it automatically uploaded and saved in the S3 bucket before I click on the submit button.

The issue is when someone added a xml file to that field and then closes the tab without submitting the form. The xml file is already uploaded to S3 bucket that way.

I need a way to manually upload this. I need to upload the image only after I pressed the submit button in the form. Without uploading the xml file automatically when selecting.

This is my buildForm() function.

public function buildForm(array $form, FormStateInterface $form_state) {

  $form['sitemap'] = [
    '#type'              => 'managed_file',
    '#name'              => 'sitemap',
    '#title'             => t('Upload Site Map (xml)'),
    '#upload_location'   => 's3://private/sitemap',
    '#field_suffix'      => 'XML format supported',
    '#upload_validators' => [
      'file_validate_extensions' => ['xml'],
    ],
    '#required'          => TRUE,
    '#validated'         => TRUE,
  ];

  $form['submit'] = [
    '#type'       => 'submit',
    '#value'      => t('Save configuration'),
    '#attributes' => [],
    '#validated'  => TRUE,
  ];

  return $form;
}
  • That should be okay imho. There was extra work done in order to achieve the functionality of auto-upload as it improves the UX process a lot. The uploaded files are saved with a status of 0 and are mostly cleared on cron run. – AjitS Jan 18 '18 at 11:36
  • @AjitS Yes true, but there are some things that I want to do before uploading the xml. I can write that in submitForm(array &$form, FormStateInterface $form_state). But the thing is it only executes after I clicked on the submit button. Is there a way to do it before auto upload happens? – i am batman Jan 18 '18 at 11:58
  • 1
    Override the #attached or remove the "file/drupal.file" library from it or write js that overrides the Drupal.behaviors.fileAutoUpload – user21641 Jan 18 '18 at 12:29
  • @IvanJaros Could you please elaborate more. I have found Drupal.behaviors.fileAutoUpload. But I don't know to edit it – i am batman Jan 19 '18 at 9:25
  • 1
    I have found a way !! Drupal has a hidden submit button for upload the file automatically. Remove that button with jquery or js so the file won't upload automatically. You can find the button with inspect element. The button has the class js-hide button js-form-submit form-submit. Peace ! – i am batman Jan 22 '18 at 5:31
1

I have stopped the managed_file auto uploading this way. This is like a workaround. But this works. You better put this in the $(document).ready in the js file.

(function($) {
    $(document).ready(function() {
        /*Drupal has a hidden submit button for upload the file 
          automatically. This will remove the button so Drupal won't auto upload for the given managed_file field*/
        $("input[name*='name_of_the_button']").remove();
    });
}(jQuery));

note - make sure you find the correct button. This is not your upload file button. This is the button that drupal creates automatically to do the file upload. This button is not visible in the page. So you need to go the inspect element and find this button. The button has the class js-hide button js-form-submit form-submit. So you won't miss !

  • It work to remove only hide button. But on saving form data, image will not be saved as its not working for me. – usmanjutt84 Nov 3 '18 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.