Modx does not copy files with the duplicate resource(( Please help to solve the problem

Hello, everyone! I have a problem when copying resources with a MIGX tv field.

I needed to implement multi-uploading of photos in my resources, and I successfully did it. Steps of my actions:

  1. I created a MIGX called resourcealbum.
  2. I created a new “Media Source” called “ResourceMediaPath”.
  3. I created a TV field called “resourcealbum” and selected the source “ResourceMediaPath”, and also provided access to the necessary templates.

Now, when I create a resource, I can upload many photos in a multi-upload, because a folder with the resource id is automatically created at the path “assets/AdditionalPhotos/1” where “1” corresponds to the ID of the created resource.

BUT I’M FACING ONE PROBLEM!

When I try to duplicate this resource that already contains these additional photos, the new resource contains the paths of the previous resource and for some reason the photos are not copied to the server((

I’m from Ukraine, I tried to find an answer on our forums, but didn’t find anything, as we have a very small MODX community. I will be very grateful for your help if you can find a way and tell me how to make sure that when copying one resource, all the files that were uploaded to it (including photos) are also copied and the paths are correct.

I’ve attached a link to Google Drive (there are screenshots and the migx json file).
https://drive.google.com/drive/folders/12McbbBJd-wUH0HWDyAYX5TcWnlGgwnfU?usp=sharing

I don’t know what to do anymore((

Maybe need to write some kind of plugin, something like this?

<?php
switch ($modx->event->name) {
    case 'OnResourceDuplicate':

        // There must be some code here that should help me fix copying my resources along with the file to the new resource id folder...

        break;
}
return;

But I do not know how to write such a plugin( I only know that there should be a very simple solution to copy resources, also copied files to the new paths(

You are probably right, that you have to write a plugin on the OnResourceDuplicate event.

In the event you get the parameters oldResource and newResource. From these parameters you can read the resource IDs. With the IDs you can construct the paths to the image folders.

Then you have to iterate over all the files in the source folder and copy them to the target folder using standard PHP functions. Maybe this helps:

Thanks)
Can I ask you to help me write this script?

Or at least tell me how to get the ID of the old resource parameter and how to get the id of the duplicate resource?

I just can’t find the full list of parameters in modx to know how to operate with them(

The parameters oldResource and newResource are objects of the class “MODX\Revolution\modResource”. So you can use them like any other resource variables:

$id_old = $oldResource->get('id');
$id_new = $newResource->get('id');
1 Like

oh) thank you) now I will try to write this plugin, I doubt that I will succeed, but I will try anyway)

I tried to write this script, it creates a folder with a duplicate resource, but it does not copy files from the folder of the previous resource(

<?php
$duplicateFiles = $modx->event->name;
switch($duplicateFiles) {
    case 'OnResourceDuplicate':
          
// Set the paths for the old and new folders
$old_folder = '../assets/AdditionalPhotos/' . $oldResource->get('id') . '';
$new_folder = '../assets/AdditionalPhotos/' . $newResource->get('id') . '';

// Check if the new folder exists, if not create it
if (!file_exists($new_folder)) {
    mkdir($new_folder, 0755, true);
}

// Loop through each file in the old folder and copy it to the new folder
foreach(glob($old_folder.'*') as $file) {
    $new_file = $new_folder.basename($file);
    copy($file, $new_file);
}
          
          
    break;
}

I believe you are missing the / at the end of your folder paths:

$old_folder = '../assets/AdditionalPhotos/' . $oldResource->get('id') . '/';
$new_folder = '../assets/AdditionalPhotos/' . $newResource->get('id') . '/';
1 Like

Yeah) I missed the slash at the end) Now files are copied perfectly when duplicating a resource)

But on the duplicated resource, the old path to tv from the previous resource is filled in. “assets/AdditionalPhotos/1/photo.jpg”, but it should be “assets/AdditionalPhotos/2/photo.jpg”
Can I also change the file paths on a duplicate resource in the same way?)

You can get the content of the TV with the code

$tv_value = $newResource->getTVValue('TV name or ID');

The content of a MIGX TV is JSON, so you have to use json_decode() or $modx->fromJSON() to get an array. Then loop over the array, change the paths and use setTVValue() to save the changed value.

1 Like

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.