flash Posted April 29 Share Posted April 29 (edited) I have hundreds of images uploaded to "field1". And then i have another field "field2" that also have hundreds of image uploaded to it. My goal is to have only one field for images. I would like to change the "field2" to "field1" if possible or add field1 to the same template and migrate or copy the images to that field. I dont want to remove any existing images. Field1 and Field2 is on separate templates. They are "single" images/field. Does anyone have some tips? Please help. Edited April 29 by Flashmaster82 Link to comment Share on other sites More sharing options...
Christophe Posted April 29 Share Posted April 29 https://processwire.com/modules/file-mover/ 1 Link to comment Share on other sites More sharing options...
flash Posted April 29 Author Share Posted April 29 (edited) Yes i know, but i have hundreds of images each on its own page... It would take forever. Edited April 29 by Flashmaster82 Link to comment Share on other sites More sharing options...
Tiberium Posted April 30 Share Posted April 30 Robin answered it on the other topic: The solution is, to use the API, not the admin interface. Link to comment Share on other sites More sharing options...
BillH Posted May 1 Share Posted May 1 As @Tiberium suggest, the best way would be to run a PHP script from an external file (how I'd do it) or in a template. Or an alternative would be to set up an Admin Action: https://processwire.com/modules/process-admin-actions/. If you're not familiar with running scripts that access the API, I suggest starting with https://processwire.com/docs/front-end/include/. And another helpful page is https://processwire.com/docs/start/api-access/. In all cases, the procedure would essentially be this. First rename field sand add them to templates manually through the admin interface (if necessary, temporarily rename fields, then name them what you finally want when everything is in the right place). Then set up a script to loop through the pages with the source of the images, copying each image from the old field to the new field on the relevant page. (If each field has only a single image, you won't need to loop through the images in the field.) 1 Link to comment Share on other sites More sharing options...
flash Posted May 1 Author Share Posted May 1 (edited) @BillH Thanks for the reply. I installed Admin Actions and did "Copy Content to Other Field" That worked. But then if i remove the original image or remove the field from that template it will remove the image on the new field as well. Pageimage: Original image does not exist to create size variation: filename.jpg The descriptions field was only copied the default language. I also have a custom field that was not copied on both default and english. Edited May 1 by flash Link to comment Share on other sites More sharing options...
BillH Posted May 1 Share Posted May 1 (edited) You will need to write your own script – either to run in Admin Actions or as a separate PHP script (say if you need guidance on how to set this up) or in a template. The Admin Actions "Copy Content..." should, I think, have duplicated the image. One quick way to check for sure if the image has been duplicated to the new page is to look in the directory /site/assets/files/ and find the directory named with the ID of the page in question. Edited May 2 by BillH Link to comment Share on other sites More sharing options...
MarkE Posted May 2 Share Posted May 2 11 hours ago, BillH said: a separate PHP script Surely the easiest way to do this is to use TracyDebugger console? Link to comment Share on other sites More sharing options...
BillH Posted May 2 Share Posted May 2 6 minutes ago, MarkE said: Surely the easiest way to do this is to use TracyDebugger console? As this is (presumably) a one-off exercise, yes, that's a good point! Link to comment Share on other sites More sharing options...
flash Posted May 2 Author Share Posted May 2 Maybe someone can explain if its that easy.. Link to comment Share on other sites More sharing options...
BillH Posted May 2 Share Posted May 2 The Tracy Debugger console might be the easiest mechanism for implementing and running the code – though the code itself will be very similar however you do it. If you don't have it installed, Tracy Debugger is a highly recommended module. The advantage of using the console is that it's easy to run code and get an output, so you can work in an interactive way, testing things and running them. I suggest trying it out with straightforward code (note that the context is the current page, so $page->title returns the title of the current page) to see if it's a way of working that'd suit you. Link to comment Share on other sites More sharing options...
flash Posted May 2 Author Share Posted May 2 (edited) I´m a beginner so i don´t know how to code it.. Edited May 2 by flash Link to comment Share on other sites More sharing options...
BillH Posted May 2 Share Posted May 2 (edited) The following should help to get you started. This is totally off the top of my head, so apologies if any of it isn't correct! $sourcePages = $pages->find("A suitable selector here"); foreach($sourcePages as $sPage) { $sPage->of(false); // Turn off output formatting // Get the image and caption $sourceImage = $sPage->myImageField->first(); if(!$sourceImage) continue; // Skip if no image $sourceCaption = $sourceImage->get('caption'); // Assumes a custom image field named 'caption' // Find the target page $targetPage = $pages->get("Create a suitable selector here"); $targetPage->of(false); // Set fields in the target page $targetPage->myImageField->add($sourceImage->filename); $newImg = $targetPage->myImageField->last(); $newImg->set('caption', $sourceCaption) // Save $targetPage->save(); } I suggest commenting out saving the target page while testing. Edited May 2 by BillH 1 Link to comment Share on other sites More sharing options...
flash Posted May 2 Author Share Posted May 2 @BillH Thanks for the reply. I will try the code out. Link to comment Share on other sites More sharing options...
MarkE Posted May 2 Share Posted May 2 3 hours ago, flash said: I will try the code out Back up your database first! 2 Link to comment Share on other sites More sharing options...
AsherBlake Posted May 7 Share Posted May 7 (edited) Combine fields by migrating images; ensures no data is lost. Spoiler To merge images from field2 into field1 without losing data, you’ll need to export images from field2 and import them into field1. Since they’re on separate templates, create a unified template with field1 only, then migrate or copy images via scripting or a bulk import tool. Avoid deleting any images during the process to keep data safe. Writing a dissertation is a huge challenge, so I decided to let experts help me write my dissertation. I chose Academized because their service at https://academized.com/write-my-dissertation is reliable and professional. They delivered a comprehensive, well-structured paper that helped me succeed academically and meet my deadline. Edited Thursday at 12:56 PM by AsherBlake 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