Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I've created a multi-step form using ctools wizard. One of my forms has a button that forces a file download using drupal's file_transfer method. This works fine until I put my form wizard into a ctools modal popup. Now when I press the "download" button on my form, I get an error message. Is there any way to force a file download from within a modal popup?
Here's my download button submit callback:
<?php
function custom_wizard_download_button_submit($form, &$form_state) {
// Start the download
$content_disposition = 'attachment; filename="' . basename($form_state['download_object']->download_uri) . '"';
$headers = array
(
'Content-type' => 'application/octet-stream',
'Content-Disposition' => $content_disposition,
'Content-Transfer-Encoding' => 'binary',
);
file_transfer($form_state['download_object']->download_uri, $headers);
}
?>
Here's part of the error message:
An AJAX HTTP error occurred.
HTTP Result Code: 200
Debugging information follows.
Path: /custom-download-counter/ajax/download/123/start-download
StatusText: OK
ResponseText: long wall of gibberish characters
Comments
Comment #0.0
JordanMagnuson CreditAttribution: JordanMagnuson commentedUpdated issue summary.
Comment #0.1
JordanMagnuson CreditAttribution: JordanMagnuson commentedUpdated issue summary.
Comment #0.2
JordanMagnuson CreditAttribution: JordanMagnuson commentedUpdated issue summary.
Comment #1
JordanMagnuson CreditAttribution: JordanMagnuson commentedComment #2
Ashlar CreditAttribution: Ashlar commentedThis support request has not been active for over two months. In an effort to clean-up the issue queue this item has been closed. If all modules are up-dated and the request is still a concern please feel free to change the Status back to active.
Comment #3
tara@neier.org CreditAttribution: tara@neier.org commentedI'm having this same problem. Is there documentation on how to work around this?
Comment #4
cpkeller CreditAttribution: cpkeller commentedsame problem here. Any examples or techniques to get file downloads working with ctools modals?
Comment #5
merlinofchaos CreditAttribution: merlinofchaos commentedThe problem is that whatever technique you're using is loading the form via AJAX. I assume you're using it in a modal?
The technique you're attempting to use to download the file assumes the user visited the page directly, not as an AJAX call. That won't work that way.
What you'll probably need to do is to have that submit actually create an ajax redirect command which will redirect the user to a URL with the proper information to download the file. Another possibility would be to issue a command to open up a new window and direct that window to the file download page. Either way, attempting to download the file directly in the submit handler will not work via AJAX.
Since the modal assumes all buttons on the form will submit via ajax, it may or may not be possible to override click handling for that button. If it is possible, you would write a little bit of javascript to submit the form not via AJAX rather than via AJAX, and that would work. Beware that this will totally break if the form fails to validate, though, so it is probably not a good option. The redirect option is better.
Comment #6.0
(not verified) CreditAttribution: commentedsmall update
Comment #7
grisendo CreditAttribution: grisendo commentedIf the only purpose of the form is to download that file, you can add a class 'ctools-use-modal-processed' to the form. This way, you will cheat the JS, which will think it already applied the behavior to that form, and won't apply. You can add this class in the function where you build the form if it's yours, or with a hook_form_alter if it isn't yours.