Jump to content

Copying an image field to another page


MarkE
 Share

Recommended Posts

I have a function which copies (selected) fields from one page to another. This was working fine until I added a field of type FieldtypeImage. I can't get this to copy across.

The original code (before adding the image type) was 

 foreach ($foundPage->fields as $field) {
                if (!in_array($field->name, $unwantedFields)) {
                    $foundPage->$field = $p->$field;
                }
 }

where $foundPage is a matching page for $p (same template) and $unwantedFields is an array of field names not to be copied.

When the image field was added to the template, no errors were reported, but the image field would not copy over.  The field $p->images seemed to be empty.

So I tried this code:

foreach ($foundPage->fields as $field) {
                $fieldName = $field->name;
                $updateField = $p->getFields()->get("name=$fieldName");
				if (!in_array($field->name, $unwantedFields)) {
 					$foundPage->$field = $updateField;
				}
}

That did populate the $updateField with the image field and showed it as a type FieldtypeImage, but I got the message "Item added to ProcessWire\Pageimages is not an allowed type"

Given that the images field is an array (and actually I would prefer to merge rather than replace the contents), I tried adding in something like:

                  if ($field->type == 'FieldtypeImage') {
                      foreach ($updateField as $image) {
                              $foundPage->$field->add($image);
                      }
                  }

but that did nothing (nor threw any errors).

Obviously there's something I don't quite understand about FieldtypeImage and/or WireArray. I read https://processwire.com/docs/fields/images/, but that didn't seem to cover it.

Any ideas what I am doing wrong?

Thanks.

Link to comment
Share on other sites

5 minutes ago, horst said:

Have you tried your code with the filename, not the image object?

->add($image->filename)

Yes - no luck with that. I've now found the docs on Pageimages (e.g. https://processwire.com/api/ref/pageimages/add/) which implies I can add a filename string or an existing Pageimage type. My field ('images') is FieldtypeImage which seems (on debugging) to be an array, but not of images, rather something like this:

[gif jpg jpeg png, 1, InputfieldImage, 0, 1, 6, Array, 1, 0, 0, sitemap, Site, 0, 0, grid, on, 0, 90, 0, 0]

I'm struggling to find what property of FieldtypeImage I should use to add to another FieldtypeImage.

Link to comment
Share on other sites

Hmm. I can see that the array I have is actually the array defining the FieldtypeImage rather than an array of a FieldtypeImage object!

EDIT

Some more clues: The copying is within an addHookAfter('Pages::added',...) hook. It seems that the path for the images is set, but that the actual images are not saved to the files directory until a later stage. So maybe I need to move the whole code to another hook? However, I only want the code to execute when the new page (with the images) is added (it is created by FormBuilder).

Link to comment
Share on other sites

UPDATE: I think I have found the problem (and a sort of solution).

The problem seems to be that the file system has not saved the image files when the hook is run. I suspect that it makes no difference what hook is used as they will all execute before the files have been saved.

So my sort-of-solution is to take the code out of the hook and instead execute it from a LazyCron (everyMinute). That's timely enough for what I need. After executing, the source page is removed so that it does not execute again.

If anybody has any better ideas, I'd be interested to know.

Link to comment
Share on other sites

1 hour ago, MarkE said:

The problem seems to be that the file system has not saved the image files when the hook is run.

You haven't said what method you are hooking, but it sounds like if the page is not yet saved the logical thing would be to either hook after Pages::saved or else to save the page within your hook before you try and get the images from it.

Link to comment
Share on other sites

The hook is on Pages::added. I tried it on Pages::saved as well. I also tried saving the page (in the first hook, not the second ? ). All the other fields were present and correct, it was just the images that were missing as they were not saved at the time the hook ran, The bar dumps indicated that the path to the files had been set, but that there were no files in that directory. Immediately afterwards (by inspection, and as picked up by my LazyCron), the files appeared.

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...