Skip to content

Commit

Permalink
Fix translatable asset controller: make sure the locale is properly p…
Browse files Browse the repository at this point in the history
…assed
  • Loading branch information
sten committed Nov 18, 2024
1 parent 5d4c2ac commit 8d1b494
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 6 deletions.
3 changes: 3 additions & 0 deletions resources/lang/en/filament-flexible-blocks-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
'form_component.name_lbl' => 'Name',
'form_component.asset_media_lbl' => 'File',
'navigation_group' => 'Settings',
'error' => [
'asset_media_not_found' => 'The asset media was not found',
],
];
3 changes: 3 additions & 0 deletions resources/lang/fr/filament-flexible-blocks-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
'form_component.name_lbl' => 'Nom',
'form_component.asset_media_lbl' => 'Fichier',
'navigation_group' => 'Preferences',
'error' => [
'asset_media_not_found' => 'Le média de la ressource n\'a pas été trouvé.',
],
];
3 changes: 3 additions & 0 deletions resources/lang/nl/filament-flexible-blocks-asset-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
'form_component.name_lbl' => 'Naam',
'form_component.asset_media_lbl' => 'Bestand',
'navigation_group' => 'Instellingen',
'error' => [
'asset_media_not_found' => 'Het bestand werd niet gevonden',
],
];
30 changes: 24 additions & 6 deletions src/Http/Controllers/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;

class AssetController
Expand All @@ -20,16 +22,32 @@ public function index(string $assetId, ?string $locale = null)
}
}

//TODO conversions
$filters = [];
if ($locale) {
$filters = ['locale' => $locale];
$assetMedia = $this->getAssetMedia($asset, $locale);

if (! $assetMedia) {
abort(Response::HTTP_NOT_FOUND, trans('filament-flexible-blocks-asset-manager::filament-flexible-blocks-asset-manager.error.asset_media_not_found'));
}

return $asset
->getFirstMedia($asset->getAssetCollection(), $filters)
return $assetMedia
->setCustomHeaders([
'X-Robots-Tag' => 'none', //equivalent to noindex, nofollow.
]);
}

private function getAssetMedia(InteractsWithMedia $asset, ?string $locale = null): ?Media
{
//TODO conversions
$assetMedia = null;
if (! $locale && FilamentFlexibleBlocksAssetManagerConfig::hasTranslatableAssets()) {
$locale = app()->getLocale();
$filters = ['locale' => $locale];
$assetMedia = $asset->getFirstMedia($asset->getAssetCollection(), $filters);
}

if (! $assetMedia) {
$assetMedia = $asset->getFirstMedia($asset->getAssetCollection());
}

return $assetMedia;
}
}
6 changes: 6 additions & 0 deletions src/Models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Spatie\MediaLibrary\InteractsWithMedia;
use Spatie\MediaLibrary\MediaCollections\Models\Media;
use Spatie\Translatable\HasTranslations;
use Statikbe\FilamentFlexibleBlocksAssetManager\FilamentFlexibleBlocksAssetManagerConfig;
use Statikbe\FilamentFlexibleContentBlocks\Models\Concerns\HasTranslatedMediaTrait;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\HasTranslatableMedia;
use Statikbe\FilamentFlexibleContentBlocks\Models\Contracts\Linkable;
Expand Down Expand Up @@ -48,6 +49,11 @@ public function getAssetCollection(): string

public function getViewUrl(?string $locale = null): string
{
//add current locale if not passed, the asset controller will use the default locale if none is passed.
if (! $locale && FilamentFlexibleBlocksAssetManagerConfig::hasTranslatableAssets()) {
$locale = app()->getLocale();
}

return route('filament-flexible-blocks-asset-manager.asset_index', [
'assetId' => $this,
'locale' => $locale,
Expand Down

0 comments on commit 8d1b494

Please sign in to comment.