Jump to content
Hurme

Creating a page and a children via API

Recommended Posts

I need to create a page and then add a children to it when user fills in a form.

I can create the parent page by putting the code on the form page itself, and then the child by using a pages::added hook and bringing the needed information to the hook via session.

Is there any more straightforward way of doing it or is this a good approach?

Share this post


Link to post
Share on other sites

Not sure I fully understand the scenario, however, technically, there is no 'adding children to a page'. Conversely, you give a child a parent 😄. This means, there is no difference between creating a parent page and its children except for specifying the parent.

// create parent
$p = new Page();
$p->template = 'basic-page';
$p->parent = $pages->get(1234);
$p->title = 'Parent Page';
$p->save();
// create child
$c = new Page();
$c->template = 'child-template';
$c->parent = $p;// new parent above
$c->title = 'Child Page';
$c->save();

It seems to me though that your question is mainly related to getting info to create the child page after some event has occurred? if that's the case, please provide more information about the form submission and handling process.

  • Like 2

Share this post


Link to post
Share on other sites

Well yes, your example is what I'm trying to do. But in this case I get an error when I'm trying to save the "child". I was assuming it was because PW couldn't save those two pages back to back in same operation, so I relegated creating and saving the child into a hook that takes place after the first page has been saved.

But your example seems to work fine, so I'm just going to go back and check my code.

Share this post


Link to post
Share on other sites

you can try to create the child page only after the parent is saved, like so:

 

// create parent
$p = new Page();
$p->template = 'basic-page';
$p->parent = $pages->get(1234);
$p->title = 'Parent Page';
if($p->save()){
    // create child
    $c = new Page();
    $c->template = 'child-template';
    $c->parent = $p;// new parent above
    $c->title = 'Child Page';
    $c->save();
}

 

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...