Jump to content

Copying File from another page via API


onjegolders
 Share

Recommended Posts

Hey guys,

I'm creating a form via the API where a user can create a page and click to "create and duplicate". The idea is that this reloads the form with a GET variable to the page just created, takes the image from that page and also adds it to the new duplicate page.

Essentially there may be 4 pages created but they should all share the same file.

Would it be necessary to create a new WireUpload on every page creation or is there some way to do the following (or some variation):

$duplicate_page->file->add($just_created_page->file);

OR

$duplicate_page->file->add($just_created_page->file->url);

I don't seem to be able to pull this off but perhaps it's because the WireUpload needs to take place on every page creation.. Anyone had any experience with this?

Thanks

Link to comment
Share on other sites

In a rush so not the cleanest code, but this seems to work for me - is this what you are looking for?

$p = $pages->get(xxxx); //id of page to copy 

$np = new Page();
$np->of(false);
$np->parent = $pages->get(1); //set parent to homepage
$np->template = "basic-page";
$np->title = 'test new page';
$np->save();
$np->images->add('http://'.$config->httpHost.$p->images->last()->url);
$np->save();
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

Back at my desk again, and thinking through this properly - I think the better option is actually:

$np->images->add($p->images->last()->filename);

You either need the full URL or the full disk path. I think that is where you were having trouble. In my first example, it wasn't working without the 'http://'.$config->httpHost part, but using "->filename" works great and is cleaner!

  • Like 4
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

×
×
  • Create New...