Skip to content

Commit df4fc1a

Browse files
committed
bugfix: cannot preview image if the field is media picker
1 parent 5cd853b commit df4fc1a

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Fields/Converters/MediaPickerConverter.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,26 @@ public function toDisplayValue(mixed $sourceValue, ?string $locale, ?string $fal
1818

1919
$formattedSourceValue = is_array($value) ? $value : [$value];
2020

21+
// fetch media assets from the database using primary keys (In Preview mode)
22+
$mediaAssetIds = collect($formattedSourceValue)->where(fn ($v) => is_string($v));
23+
24+
$mediaAssets = (count($mediaAssetIds) > 0) ?
25+
collect(inspirecms_asset()->findByKeys($mediaAssetIds->all()))->mapWithKeys(fn ($item) => [$item->getKey() => $item]) :
26+
collect();
27+
2128
return collect($formattedSourceValue)
22-
->map(function ($item) {
29+
->map(function ($item) use ($mediaAssets) {
2330
if (is_array($item)) {
2431
return MediaAssetDto::fromArray($item);
2532
} elseif ($item instanceof MediaAsset) {
2633
return MediaAssetDto::fromModel($item);
2734
} elseif ($item instanceof BaseDto) {
2835
return $item;
36+
} elseif (is_string($item)) {
37+
$model = $mediaAssets->get($item);
38+
if ($model) {
39+
return MediaAssetDto::fromModel($model);
40+
}
2941
}
3042

3143
return null;

0 commit comments

Comments
 (0)