Jump to content

ProcessPageAdd: New page must be saved before files can be accessed from it


bernhard
 Share

Recommended Posts

hi guys, i have the following saveReady hook: 

public function createCodeFile($event) {
    $page = $event->arguments('page'); 
    if($page->template == 'code' OR $page->template == 'treenode') {

        // create file if textarea is not empty
        if($page->code) {
            $filename = "tmpupload/code" . $page->id . "_" . $page->name . ".inc";
            file_put_contents($filename, $page->code);
            $page->codefile->removeAll();
            $page->save('codefile');
            $page->codefile->add($filename);
            unlink($filename);
        }
        else {
            $page->codefile->removeAll();
            $page->save('codefile');
        }

    }

works great for editing pages, but when i create a new page, i get the error

New page '/simple-map/functions/test-test/' must be saved before files can be accessed from it

the problem is in the else-block:

else {
    $page->codefile->removeAll();
    $page->save('codefile');
}

how can i check if "codefile" is accessible? or should i use another hook? if i hooked after "saved" i would have to save the page on my own and that would lead to an endless loop, wouldn't it?

thanks for your help!

Link to comment
Share on other sites

I used a hidden storage page as workaround. It's also used to build the FormHelperExtra module (proof of concept, first draft after separating additional features from FormHelper module).

Hidden page is created / deleted during module install / uninstall. If the page isn't saved files are added to the storage page object. During page save you can move the files from storage to the new page (add() & unlink()). Should work fine... You can use the experimental code as example. FormHelperExtra module should work, but code will changed / optimized in the future.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...