Jump to content

Add page with multi image field content


olafgleba
 Share

Recommended Posts

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

@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();

 

  • Like 2
Link to comment
Share on other sites

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);
}

 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

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(); .

  • Like 2
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...