Skip to content

Commit 8d1b494

Browse files
committed
Fix translatable asset controller: make sure the locale is properly passed
1 parent 5d4c2ac commit 8d1b494

File tree

5 files changed

+39
-6
lines changed

5 files changed

+39
-6
lines changed

resources/lang/en/filament-flexible-blocks-asset-manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
'form_component.name_lbl' => 'Name',
88
'form_component.asset_media_lbl' => 'File',
99
'navigation_group' => 'Settings',
10+
'error' => [
11+
'asset_media_not_found' => 'The asset media was not found',
12+
],
1013
];

resources/lang/fr/filament-flexible-blocks-asset-manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
'form_component.name_lbl' => 'Nom',
88
'form_component.asset_media_lbl' => 'Fichier',
99
'navigation_group' => 'Preferences',
10+
'error' => [
11+
'asset_media_not_found' => 'Le média de la ressource n\'a pas été trouvé.',
12+
],
1013
];

resources/lang/nl/filament-flexible-blocks-asset-manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
'form_component.name_lbl' => 'Naam',
88
'form_component.asset_media_lbl' => 'Bestand',
99
'navigation_group' => 'Instellingen',
10+
'error' => [
11+
'asset_media_not_found' => 'Het bestand werd niet gevonden',
12+
],
1013
];

src/Http/Controllers/AssetController.php

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Illuminate\Http\Response;
66
use Illuminate\Support\Facades\Gate;
7+
use Spatie\MediaLibrary\InteractsWithMedia;
8+
use Spatie\MediaLibrary\MediaCollections\Models\Media;
79
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
810

911
class AssetController
@@ -20,16 +22,32 @@ public function index(string $assetId, ?string $locale = null)
2022
}
2123
}
2224

23-
//TODO conversions
24-
$filters = [];
25-
if ($locale) {
26-
$filters = ['locale' => $locale];
25+
$assetMedia = $this->getAssetMedia($asset, $locale);
26+
27+
if (! $assetMedia) {
28+
abort(Response::HTTP_NOT_FOUND, trans('filament-flexible-blocks-asset-manager::filament-flexible-blocks-asset-manager.error.asset_media_not_found'));
2729
}
2830

29-
return $asset
30-
->getFirstMedia($asset->getAssetCollection(), $filters)
31+
return $assetMedia
3132
->setCustomHeaders([
3233
'X-Robots-Tag' => 'none', //equivalent to noindex, nofollow.
3334
]);
3435
}
36+
37+
private function getAssetMedia(InteractsWithMedia $asset, ?string $locale = null): ?Media
38+
{
39+
//TODO conversions
40+
$assetMedia = null;
41+
if (! $locale && FilamentFlexibleBlocksAssetManagerConfig::hasTranslatableAssets()) {
42+
$locale = app()->getLocale();
43+
$filters = ['locale' => $locale];
44+
$assetMedia = $asset->getFirstMedia($asset->getAssetCollection(), $filters);
45+
}
46+
47+
if (! $assetMedia) {
48+
$assetMedia = $asset->getFirstMedia($asset->getAssetCollection());
49+
}
50+
51+
return $assetMedia;
52+
}
3553
}

src/Models/Asset.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Spatie\MediaLibrary\InteractsWithMedia;
99
use Spatie\MediaLibrary\MediaCollections\Models\Media;
1010
use Spatie\Translatable\HasTranslations;
11+
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
1112
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasTranslatedMediaTrait;
1213
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasTranslatableMedia;
1314
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\Linkable;
@@ -48,6 +49,11 @@ public function getAssetCollection(): string
4849

4950
public function getViewUrl(?string $locale = null): string
5051
{
52+
//add current locale if not passed, the asset controller will use the default locale if none is passed.
53+
if (! $locale && FilamentFlexibleBlocksAssetManagerConfig::hasTranslatableAssets()) {
54+
$locale = app()->getLocale();
55+
}
56+
5157
return route('filament-flexible-blocks-asset-manager.asset_index', [
5258
'assetId' => $this,
5359
'locale' => $locale,

0 commit comments

Comments
 (0)