1

I'm writing a plugin that gathers up all the assets from an entry and saves it out as a zip (along with a JSON file with information from the entry). Any advice regarding a better method is appreciated, but right now I add the code in on('entries.saveEntry'), like so:

    craft()->on('entries.saveEntry', function(Event $event) {
        $entry = $event->params['entry'];
        $outputArray = array();

        /* 
        business logic filling variables removed for the sake of brevity 
        */

        if (($entry['sectionId'] == 3) && ($entry['buildPage'] == 1)) {
            file_put_contents(getcwd()."/build/videos.json", json_encode($outputArray));

            $assetGroup[] = getcwd()."/build/videos.json";
            $zipfile = craft()->buildPage->create_zip($assetGroup, $entry['uri'], getcwd()."/build/");
            $filename = substr($zipfile,strrpos($zipfile,'/')+1);

            if (file_exists($zipfile)) {
                craft()->request->sendFile($filename, file_get_contents( $zipfile ), array('forceDownload' => true), false);
            }
        }
    });

Everything works, except that my sendFile call prevents the user from being returned to the Entries page (or stating a new entry, etc.), if that was their intent. Is it possible to capture where they were headed before I stopped them, so that I can send them on their way? Or is that not possible the way I have things written?

1 Answer 1

2

The 4th parameter to HttpRequestService->sendFile() is $terminate = true, which will by default, terminate the request after the file is sent.

Set that to false and let the request continue as usual and you should be set.

3
  • Thanks Brad, but I did have it set to false. I tried changing it to true, just to see, and it throws an error: "File not found Firefox can't find the file at /admin/entries/videoGroup/4-videotest."
    – Mason
    Commented Apr 23, 2015 at 20:24
  • What if you call $this->redirectToPostedUrl() from your controller after the file is sent?
    – Brad Bell
    Commented Apr 23, 2015 at 20:35
  • No luck with that yet, but I'll keep tweaking and post back if I get a working solution. Thanks!
    – Mason
    Commented Apr 23, 2015 at 23:02

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.