Skip to content

Commit cacbb17

Browse files
ryanmitchellclaude
andauthored
[6.x] Add option to copy data when replacing an asset (#15865)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 8432a30 commit cacbb17

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

‎lang/en/messages.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
'asset_container_warm_intelligent_instructions' => 'Automatically generate appropriate presets on upload.',
1616
'asset_container_warm_presets_instructions' => 'Specify which presets to generate automatically on upload.',
1717
'asset_replace_confirmation' => 'References to this asset in content will be updated to the selected asset.',
18+
'asset_replace_copy_data_instructions' => 'Copy the original asset\'s data (e.g. alt text, focal point) to the new asset.',
1819
'asset_reupload_confirmation' => 'Are you sure you want to re-upload this asset?',
1920
'asset_reupload_warning' => 'You may encounter browser or server caching issues. Consider replacing the asset instead.',
2021
'blueprints_hidden_instructions' => 'Hides the blueprint from create buttons in the Control Panel.',

‎src/Actions/ReplaceAsset.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public function run($assets, $values)
5050

5151
$newAsset = Facades\Asset::find($this->context['container'].'::'.$values['new_asset']);
5252

53+
if ($values['copy_data'] ?? false) {
54+
$newAsset->merge($originalAsset->data()->all())->save();
55+
}
56+
5357
$newAsset->replace($originalAsset, $values['delete_original']);
5458

5559
return [
@@ -81,6 +85,12 @@ function ($attribute, $value, $fail) use ($asset) {
8185
'allow_uploads' => true,
8286
'show_filename' => true,
8387
],
88+
'copy_data' => [
89+
'display' => __('Copy Data'),
90+
'instructions' => __('statamic::messages.asset_replace_copy_data_instructions'),
91+
'type' => 'toggle',
92+
'default' => true,
93+
],
8494
'delete_original' => [
8595
'display' => __('Delete Original Asset'),
8696
'type' => 'toggle',

‎tests/Actions/ReplaceAssetTest.php‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Tests\Actions;
4+
5+
use Illuminate\Http\UploadedFile;
6+
use Illuminate\Support\Facades\Storage;
7+
use PHPUnit\Framework\Attributes\Test;
8+
use Statamic\Actions\ReplaceAsset;
9+
use Statamic\Assets\AssetContainer;
10+
use Statamic\Facades\Asset;
11+
use Tests\PreventSavingStacheItemsToDisk;
12+
use Tests\TestCase;
13+
14+
class ReplaceAssetTest extends TestCase
15+
{
16+
use PreventSavingStacheItemsToDisk;
17+
18+
private $container;
19+
20+
public function setUp(): void
21+
{
22+
parent::setUp();
23+
24+
Storage::fake('test');
25+
26+
$this->container = tap(
27+
(new AssetContainer)->handle('test_container')->disk('test')
28+
)->save();
29+
}
30+
31+
private function createAsset($filename, $yaml = null)
32+
{
33+
$file = UploadedFile::fake()->image($filename, 30, 60);
34+
Storage::disk('test')->putFileAs('path/to', $file, $filename);
35+
36+
if ($yaml) {
37+
Storage::disk('test')->put('path/to/.meta/'.$filename.'.yaml', $yaml);
38+
}
39+
}
40+
41+
private function runAction(array $values)
42+
{
43+
return (new ReplaceAsset)
44+
->context(['container' => 'test_container'])
45+
->run(collect([Asset::find('test_container::path/to/original.jpg')]), $values);
46+
}
47+
48+
#[Test]
49+
public function it_copies_data_from_the_original_asset()
50+
{
51+
$this->createAsset('original.jpg', "data:\n alt: 'Original alt'\n copyright: 'Someone'\n focus: 25-75-1");
52+
$this->createAsset('new.jpg', "data:\n alt: 'New alt'\n caption: 'New caption'");
53+
54+
$this->runAction([
55+
'new_asset' => 'path/to/new.jpg',
56+
'copy_data' => true,
57+
'delete_original' => true,
58+
]);
59+
60+
$this->assertNull($this->container->asset('path/to/original.jpg'));
61+
$this->assertEquals([
62+
'alt' => 'Original alt',
63+
'caption' => 'New caption',
64+
'copyright' => 'Someone',
65+
'focus' => '25-75-1',
66+
], $this->container->asset('path/to/new.jpg')->data()->sortKeys()->all());
67+
}
68+
69+
#[Test]
70+
public function it_does_not_copy_data_when_disabled()
71+
{
72+
$this->createAsset('original.jpg', "data:\n alt: 'Original alt'");
73+
$this->createAsset('new.jpg');
74+
75+
$this->runAction([
76+
'new_asset' => 'path/to/new.jpg',
77+
'copy_data' => false,
78+
'delete_original' => false,
79+
]);
80+
81+
$this->assertEquals(['alt' => 'Original alt'], $this->container->asset('path/to/original.jpg')->data()->all());
82+
$this->assertEquals([], $this->container->asset('path/to/new.jpg')->data()->all());
83+
}
84+
}

0 commit comments

Comments
 (0)