Skip to content

Commit

Permalink
Support multiple alts (#74)
Browse files Browse the repository at this point in the history
* Support multiple alts

* Refactor a little.

---------

Co-authored-by: Duncan McClean <[email protected]>
  • Loading branch information
carstenjaksch and duncanmcclean authored Jan 7, 2025
1 parent e92a698 commit dc06fd8
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Transformers/AssetsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public function transform(string $value): null|string|array
$value = collect(json_decode($value, true))->join('|');
}

$assets = collect(explode('|', $value))->map(function ($path) use ($assetContainer, $relatedField, $baseUrl) {
$assetAlts = $this->config('alt')
? collect(explode('|', Arr::get($this->values, $this->config('alt'))))
: null;

$assets = collect(explode('|', $value))->map(function ($path, $index) use ($assetContainer, $relatedField, $baseUrl, $assetAlts) {
$path = Str::of($path)
->when($relatedField === 'url' && $baseUrl, function ($str) use ($baseUrl) {
return $str->after($baseUrl);
Expand Down Expand Up @@ -62,8 +66,8 @@ public function transform(string $value): null|string|array
$asset->save();
}

if ($alt = $this->config('alt')) {
$asset?->set('alt', Arr::get($this->values, $alt))->save();
if ($assetAlts) {
$asset?->set('alt', $assetAlts->get($index))->save();
}

return $asset?->path();
Expand Down

0 comments on commit dc06fd8

Please sign in to comment.