Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update asset references in links inside bard #4152

Merged
merged 2 commits into from
Aug 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 58 additions & 2 deletions src/Assets/AssetReferenceUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -168,7 +168,19 @@ 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);
}

/**
* 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();

Expand Down Expand Up @@ -205,4 +217,48 @@ protected function updateStatamicUrlsInBardImage($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();

$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;
}
}
20 changes: 20 additions & 0 deletions tests/Listeners/UpdateAssetReferencesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
],
],
],
],
[
Expand All @@ -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 */
Expand All @@ -436,7 +452,9 @@ public function it_updates_asset_references_in_bard_field_when_saved_as_html()
<img src="statamic://asset::test_container::hoff.jpg">
<img src="statamic://asset::test_container::hoff.jpg" alt="test">
</p>More text.</p>
<p><a href="statamic://asset::test_container::hoff.jpg">Link</a></p>
<img src="statamic://asset::test_container::norris.jpg">
<p><a href="statamic://asset::test_container::norris.jpg">Link</a></p>
EOT;

$entry = tap(Facades\Entry::make()->collection($collection)->data(['bardo' => $content]))->save();
Expand All @@ -450,7 +468,9 @@ public function it_updates_asset_references_in_bard_field_when_saved_as_html()
<img src="statamic://asset::test_container::content/hoff-new.jpg">
<img src="statamic://asset::test_container::content/hoff-new.jpg" alt="test">
</p>More text.</p>
<p><a href="statamic://asset::test_container::content/hoff-new.jpg">Link</a></p>
<img src="statamic://asset::test_container::norris.jpg">
<p><a href="statamic://asset::test_container::norris.jpg">Link</a></p>
EOT;

$this->assertEquals($expected, $entry->fresh()->get('bardo'));
Expand Down