From 5198b3defb0a3c4820c1bd487edaf37b9fd5e1e5 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 19 Aug 2021 15:51:54 -0400 Subject: [PATCH 1/2] Update asset references in links inside bard --- src/Assets/AssetReferenceUpdater.php | 48 ++++++++++++++++++- tests/Listeners/UpdateAssetReferencesTest.php | 20 ++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/src/Assets/AssetReferenceUpdater.php b/src/Assets/AssetReferenceUpdater.php index 0504755ed6..c0704dda6a 100644 --- a/src/Assets/AssetReferenceUpdater.php +++ b/src/Assets/AssetReferenceUpdater.php @@ -81,7 +81,7 @@ protected function updateBardFieldValues($fields, $dottedPrefix) ->each(function ($field) use ($dottedPrefix) { $field->get('save_html') === true ? $this->updateStatamicUrlsInStringValue($field, $dottedPrefix) - : $this->updateStatamicUrlsInBardImage($field, $dottedPrefix); + : $this->updateStatamicUrlsInArrayValue($field, $dottedPrefix); }); return $this; @@ -168,7 +168,13 @@ protected function updateStatamicUrlsInStringValue($field, $dottedPrefix) * @param \Statamic\Fields\Field $field * @param null|string $dottedPrefix */ - protected function updateStatamicUrlsInBardImage($field, $dottedPrefix) + protected function updateStatamicUrlsInArrayValue($field, $dottedPrefix) + { + $this->updateStatamicUrlsInImageNodes($field, $dottedPrefix); + $this->updateStatamicUrlsInLinkNodes($field, $dottedPrefix); + } + + private function updateStatamicUrlsInImageNodes($field, $dottedPrefix) { $data = $this->item->data()->all(); @@ -205,4 +211,42 @@ protected function updateStatamicUrlsInBardImage($field, $dottedPrefix) $this->updated = true; } + + private function updateStatamicUrlsInLinkNodes($field, $dottedPrefix) + { + $data = $this->item->data()->all(); + + $dottedKey = $dottedPrefix.$field->handle(); + + $bardPayload = Arr::get($data, $dottedKey, []); + + $changed = collect(Arr::dot($bardPayload)) + ->filter(function ($value, $key) { + return preg_match('/(.*)\.(type)/', $key) && $value === 'link'; + }) + ->mapWithKeys(function ($value, $key) use ($bardPayload) { + $key = str_replace('.type', '.attrs.href', $key); + + return [$key => Arr::get($bardPayload, $key)]; + }) + ->filter(function ($value) { + return $value === "statamic://asset::{$this->container}::{$this->originalValue}"; + }) + ->map(function ($value) { + return "statamic://asset::{$this->container}::{$this->newValue}"; + }) + ->each(function ($value, $key) use (&$bardPayload) { + Arr::set($bardPayload, $key, $value); + }); + + if ($changed->isEmpty()) { + return; + } + + Arr::set($data, $dottedKey, $bardPayload); + + $this->item->data($data); + + $this->updated = true; + } } diff --git a/tests/Listeners/UpdateAssetReferencesTest.php b/tests/Listeners/UpdateAssetReferencesTest.php index be92b2fec6..28f83b40b8 100644 --- a/tests/Listeners/UpdateAssetReferencesTest.php +++ b/tests/Listeners/UpdateAssetReferencesTest.php @@ -375,6 +375,12 @@ public function it_updates_asset_references_in_bard_field() 'alt' => 'hoff', ], ], + [ + 'type' => 'link', + 'attrs' => [ + 'href' => 'statamic://asset::test_container::hoff.jpg', + ], + ], [ 'type' => 'paragraph', 'content' => 'unrelated', @@ -391,6 +397,12 @@ public function it_updates_asset_references_in_bard_field() 'alt' => 'norris', ], ], + [ + 'type' => 'link', + 'attrs' => [ + 'href' => 'statamic://asset::test_container::norris.jpg', + ], + ], ], ], [ @@ -402,15 +414,19 @@ public function it_updates_asset_references_in_bard_field() $this->assertEquals('asset::test_container::hoff.jpg', Arr::get($entry->data(), 'bardo.0.content.0.attrs.src')); $this->assertEquals('hoff', Arr::get($entry->data(), 'bardo.0.content.0.attrs.alt')); + $this->assertEquals('statamic://asset::test_container::hoff.jpg', Arr::get($entry->data(), 'bardo.0.content.1.attrs.href')); $this->assertEquals('asset::test_container::norris.jpg', Arr::get($entry->data(), 'bardo.1.content.0.attrs.src')); $this->assertEquals('norris', Arr::get($entry->data(), 'bardo.1.content.0.attrs.alt')); + $this->assertEquals('statamic://asset::test_container::norris.jpg', Arr::get($entry->data(), 'bardo.1.content.1.attrs.href')); $this->assetHoff->path('content/hoff-new.jpg')->save(); $this->assertEquals('asset::test_container::content/hoff-new.jpg', Arr::get($entry->fresh()->data(), 'bardo.0.content.0.attrs.src')); $this->assertEquals('hoff', Arr::get($entry->fresh()->data(), 'bardo.0.content.0.attrs.alt')); + $this->assertEquals('statamic://asset::test_container::content/hoff-new.jpg', Arr::get($entry->fresh()->data(), 'bardo.0.content.1.attrs.href')); $this->assertEquals('asset::test_container::norris.jpg', Arr::get($entry->fresh()->data(), 'bardo.1.content.0.attrs.src')); $this->assertEquals('norris', Arr::get($entry->fresh()->data(), 'bardo.1.content.0.attrs.alt')); + $this->assertEquals('statamic://asset::test_container::norris.jpg', Arr::get($entry->fresh()->data(), 'bardo.1.content.1.attrs.href')); } /** @test */ @@ -436,7 +452,9 @@ public function it_updates_asset_references_in_bard_field_when_saved_as_html() test

More text.

+

Link

+

Link

EOT; $entry = tap(Facades\Entry::make()->collection($collection)->data(['bardo' => $content]))->save(); @@ -450,7 +468,9 @@ public function it_updates_asset_references_in_bard_field_when_saved_as_html() test

More text.

+

Link

+

Link

EOT; $this->assertEquals($expected, $entry->fresh()->get('bardo')); From 889ef22d1b50988e43e343bed80d5f09c17788f3 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 19 Aug 2021 15:53:55 -0400 Subject: [PATCH 2/2] docblocks --- src/Assets/AssetReferenceUpdater.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/Assets/AssetReferenceUpdater.php b/src/Assets/AssetReferenceUpdater.php index c0704dda6a..1d51e044a6 100644 --- a/src/Assets/AssetReferenceUpdater.php +++ b/src/Assets/AssetReferenceUpdater.php @@ -174,6 +174,12 @@ protected function updateStatamicUrlsInArrayValue($field, $dottedPrefix) $this->updateStatamicUrlsInLinkNodes($field, $dottedPrefix); } + /** + * Update asset references in bard image nodes. + * + * @param \Statamic\Fields\Field $field + * @param null|string $dottedPrefix + */ private function updateStatamicUrlsInImageNodes($field, $dottedPrefix) { $data = $this->item->data()->all(); @@ -212,6 +218,12 @@ private function updateStatamicUrlsInImageNodes($field, $dottedPrefix) $this->updated = true; } + /** + * Update asset references in bard link nodes. + * + * @param \Statamic\Fields\Field $field + * @param null|string $dottedPrefix + */ private function updateStatamicUrlsInLinkNodes($field, $dottedPrefix) { $data = $this->item->data()->all();