3

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;
}
5
  • 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
    Commented Jan 18, 2018 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? Commented Jan 18, 2018 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
    Commented Jan 18, 2018 at 12:29
  • @IvanJaros Could you please elaborate more. I have found Drupal.behaviors.fileAutoUpload. But I don't know to edit it Commented Jan 19, 2018 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 ! Commented Jan 22, 2018 at 5:31

2 Answers 2

4

Try overriding the #attached library on the managed file field:

$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,
'#attached'          => [
   'library' => [],
 ]  

];

1
  • 1
    This works perfectly.
    – Geat
    Commented Sep 18, 2019 at 22:56
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 !

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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