Jump to content

Recommended Posts

Posted

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!

  • Like 1
Posted

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.

Posted

Check for the id of the page. It won't have one before the page is saved.

once more outstanding support in the forum. thank you!

also thank you pwFoo but a simple if($page->id) did it :)

  • Like 2
Posted

Hi Bernhard,

haven't read mindful... I thought you try to save / create a new page with a uploaded file. Your case is much easier to handle with the mentioned page id check.

  • Like 1

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
  • Recently Browsing   0 members

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