olafgleba Posted July 4, 2023 Share Posted July 4, 2023 Hi, should be simple, but surely i overlook something. I copy/add a page through the API. The page to be copied holds a field for multiple images. Format for the image field is set to `Automatic` with a maximum amount of 5 (so i always get a array). Example below works quite well except the images are physically not get copied to the new page asset files folder. Tried this with a field that holds a single image with no success also. It doesn't seem to be a problem with file permissions as i am able to edit/insert images in the backend without problems. Btw, `allow_url_fopen` is on, PW 3.0.120. What i am missing? Thx, Olaf // Get page to be copied $sourcePage = $pages->findOne(...); // Init new page $page_add = wire(new Page()); // Set some basics $p_add->template = '<template-name>'; $p_add->parent = '</path/to/parent/>'; // Add some fields to copy $page_add->field = $sourcePage->field; ... // Save page to i get an ID $page_add->save(); // Although not neccessary, make sure we are working with array format $page_add->of(false); // Trying to add/copy the images to the new page $page_add->field->add($sourcePage->field); // For the sake of it, save page again $page_add->save(); Refering to Ryans comment in the Post below. Link to comment Share on other sites More sharing options...
netcarver Posted July 4, 2023 Share Posted July 4, 2023 @Olaf looks like there are a few issues with your code. Could you try this instead? // Get page to be copied $sourcePage = $pages->findOne(...); // Init new page $page_add = new Page(); // Set some basics $page_add->template = '<template-name>'; $page_add->parent = '</path/to/parent/>'; // Add some fields to copy $page_add->field = $sourcePage->field; ... // Save page to i get an ID $page_add->save(); // Although not neccessary, make sure we are working with array format $page_add->of(false); // Trying to add/copy the images to the new page $page_add->field->add($sourcePage->field); // For the sake of it, save page again $page_add->save(); 2 Link to comment Share on other sites More sharing options...
olafgleba Posted July 4, 2023 Author Share Posted July 4, 2023 @netcarver, thanks for digging in. As far as i can see, your code only differs how the new page is initialized. Correct? Tried this, no luck. The page is applied, the corresponding asset files folder also, but without the added/copied files. Link to comment Share on other sites More sharing options...
Robin S Posted July 5, 2023 Share Posted July 5, 2023 The images field value will be a Pageimages object. So when you do... $page_add->field->add() ...this is Pageimages::add(), and according to the documentation for that method it accepts either a Pageimage object or a path to an image file. You can't supply a Pageimages object to Pageimages::add(). So the relevant part of your code would need to be: foreach($page->field as $pageimage) { $page_add->field->add($pageimage); } 3 1 Link to comment Share on other sites More sharing options...
olafgleba Posted July 5, 2023 Author Share Posted July 5, 2023 Thank you @Robin S . Thats it. Umpf, so obious... Interestingly i did that in the first place. But this was before i realized i have to save the new page before adding images. As i got no results at that time, i tried other solutions. And forgot to test this again after i applied the first $page_add->save(); . 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now