Jump to content

Migrate images to another field


flash
 Share

Recommended Posts

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 by Flashmaster82
Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

Posted (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 by flash
Link to comment
Share on other sites

Posted (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 by BillH
Link to comment
Share on other sites

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

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

Posted (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 by BillH
  • Like 1
Link to comment
Share on other sites

Posted (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 by AsherBlake
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...