From 73afe9b48100efc3ed257501c5e1b8f429d0a1bb Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 12 Nov 2026 11:48:52 +0000 Subject: [PATCH] Add option to copy data when replacing an asset Co-Authored-By: Claude Opus 5.5 --- lang/en/messages.php | 1 + src/Actions/ReplaceAsset.php | 10 ++++ tests/Actions/ReplaceAssetTest.php | 84 ++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 tests/Actions/ReplaceAssetTest.php diff --git a/lang/en/messages.php b/lang/en/messages.php index 0508f562edc..35d4b44c545 100644 --- a/lang/en/messages.php +++ b/lang/en/messages.php @@ -15,6 +15,7 @@ 'asset_container_warm_intelligent_instructions' => 'Automatically generate appropriate presets on upload.', 'asset_container_warm_presets_instructions' => 'Specify which presets to generate automatically on upload.', 'asset_replace_confirmation' => 'References to this asset in content will be updated to the selected asset.', + 'asset_replace_copy_data_instructions' => 'Copy the original asset\'s data (e.g. alt text, focal point) to the new asset.', 'asset_reupload_confirmation' => 'Are you sure you want to re-upload this asset?', 'asset_reupload_warning' => 'You may encounter browser or server caching issues. Consider replacing the asset instead.', 'blueprints_hidden_instructions' => 'Hides the blueprint from create buttons in the Control Panel.', diff --git a/src/Actions/ReplaceAsset.php b/src/Actions/ReplaceAsset.php index c11d4fb52f7..a3f29fb0328 100644 --- a/src/Actions/ReplaceAsset.php +++ b/src/Actions/ReplaceAsset.php @@ -49,6 +49,10 @@ public function run($assets, $values) $newAsset = Facades\Asset::find($this->context['container'].'::'.$values['new_asset']); + if ($values['copy_data'] ?? false) { + $newAsset->merge($originalAsset->data()->all())->save(); + } + $newAsset->replace($originalAsset, $values['delete_original']); return [ @@ -71,6 +75,12 @@ protected function fieldItems() 'allow_uploads' => true, 'show_filename' => true, ], + 'copy_data' => [ + 'display' => __('Copy Data'), + 'instructions' => __('statamic::messages.asset_replace_copy_data_instructions'), + 'type' => 'toggle', + 'default' => true, + ], 'delete_original' => [ 'display' => __('Delete Original Asset'), 'type' => 'toggle', diff --git a/tests/Actions/ReplaceAssetTest.php b/tests/Actions/ReplaceAssetTest.php new file mode 100644 index 00000000000..01e5cda4a7b --- /dev/null +++ b/tests/Actions/ReplaceAssetTest.php @@ -0,0 +1,84 @@ +container = tap( + (new AssetContainer)->handle('test_container')->disk('test') + )->save(); + } + + private function createAsset($filename, $yaml = null) + { + $file = UploadedFile::fake()->image($filename, 30, 60); + Storage::disk('test')->putFileAs('path/to', $file, $filename); + + if ($yaml) { + Storage::disk('test')->put('path/to/.meta/'.$filename.'.yaml', $yaml); + } + } + + private function runAction(array $values) + { + return (new ReplaceAsset) + ->context(['container' => 'test_container']) + ->run(collect([Asset::find('test_container::path/to/original.jpg')]), $values); + } + + #[Test] + public function it_copies_data_from_the_original_asset() + { + $this->createAsset('original.jpg', "data:\n alt: 'Original alt'\n copyright: 'Someone'\n focus: 25-75-1"); + $this->createAsset('new.jpg', "data:\n alt: 'New alt'\n caption: 'New caption'"); + + $this->runAction([ + 'new_asset' => 'path/to/new.jpg', + 'copy_data' => true, + 'delete_original' => true, + ]); + + $this->assertNull($this->container->asset('path/to/original.jpg')); + $this->assertEquals([ + 'alt' => 'Original alt', + 'caption' => 'New caption', + 'copyright' => 'Someone', + 'focus' => '25-75-1', + ], $this->container->asset('path/to/new.jpg')->data()->sortKeys()->all()); + } + + #[Test] + public function it_does_not_copy_data_when_disabled() + { + $this->createAsset('original.jpg', "data:\n alt: 'Original alt'"); + $this->createAsset('new.jpg'); + + $this->runAction([ + 'new_asset' => 'path/to/new.jpg', + 'copy_data' => false, + 'delete_original' => false, + ]); + + $this->assertEquals(['alt' => 'Original alt'], $this->container->asset('path/to/original.jpg')->data()->all()); + $this->assertEquals([], $this->container->asset('path/to/new.jpg')->data()->all()); + } +}