|
3 | 3 | namespace Moox\Media\Http\Livewire; |
4 | 4 |
|
5 | 5 | use Exception; |
6 | | -use Filament\Forms\Components\FileUpload; |
7 | | -use Filament\Forms\Components\Select; |
8 | | -use Filament\Forms\Concerns\InteractsWithForms; |
9 | | -use Filament\Forms\Contracts\HasForms; |
10 | | -use Filament\Notifications\Notification; |
11 | | -use Filament\Schemas\Schema; |
12 | | -use Illuminate\Database\Eloquent\Model; |
13 | 6 | use Livewire\Component; |
14 | | -use Livewire\WithFileUploads; |
| 7 | +use Filament\Schemas\Schema; |
15 | 8 | use Livewire\WithPagination; |
16 | 9 | use Moox\Media\Models\Media; |
| 10 | +use Livewire\WithFileUploads; |
| 11 | +use Filament\Forms\Components\Select; |
| 12 | +use Filament\Forms\Contracts\HasForms; |
17 | 13 | use Moox\Media\Models\MediaCollection; |
| 14 | +use Illuminate\Database\Eloquent\Model; |
| 15 | +use Filament\Notifications\Notification; |
| 16 | +use Filament\Forms\Components\FileUpload; |
| 17 | +use Moox\Localization\Models\Localization; |
| 18 | +use Filament\Forms\Concerns\InteractsWithForms; |
18 | 19 | use Spatie\MediaLibrary\MediaCollections\FileAdderFactory; |
19 | 20 |
|
20 | 21 | /** @property \Filament\Schemas\Schema $form */ |
@@ -111,11 +112,36 @@ public function form(Schema $schema): Schema |
111 | 112 | { |
112 | 113 | $collection = Select::make('media_collection_id') |
113 | 114 | ->label(__('media::fields.collection')) |
114 | | - ->options(fn () => MediaCollection::whereHas('translations', function ($query) { |
115 | | - $query->where('locale', app()->getLocale()); |
116 | | - })->get()->pluck('name', 'id')->filter()->toArray()) |
| 115 | + ->options(function () { |
| 116 | + $currentLang = app()->getLocale(); |
| 117 | + |
| 118 | + $defaultLocale = null; |
| 119 | + if (class_exists(Localization::class)) { |
| 120 | + $localization = Localization::where('is_default', true) |
| 121 | + ->where('is_active_admin', true) |
| 122 | + ->with('language') |
| 123 | + ->first(); |
| 124 | + |
| 125 | + if ($localization && $localization->language) { |
| 126 | + $defaultLocale = $localization->locale_variant ?: $localization->language->alpha2; |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return MediaCollection::with('translations') |
| 131 | + ->get() |
| 132 | + ->mapWithKeys(function ($item) use ($currentLang, $defaultLocale) { |
| 133 | + $name = |
| 134 | + $item->translate($currentLang)?->name |
| 135 | + ?? ($defaultLocale ? $item->translate($defaultLocale)?->name : null) |
| 136 | + ?? $item->translations->first()?->name |
| 137 | + ?? ('ID: ' . $item->id); |
| 138 | + |
| 139 | + return [$item->id => $name]; |
| 140 | + }) |
| 141 | + ->toArray(); |
| 142 | + }) |
117 | 143 | ->searchable() |
118 | | - ->default(MediaCollection::first()->id) |
| 144 | + ->default(MediaCollection::first()?->id) |
119 | 145 | ->required() |
120 | 146 | ->live(); |
121 | 147 |
|
@@ -471,9 +497,32 @@ public function render() |
471 | 497 | ->orderBy('created_at', 'desc') |
472 | 498 | ->paginate(18); |
473 | 499 |
|
474 | | - $collectionOptions = MediaCollection::whereHas('translations', function ($query) { |
475 | | - $query->where('locale', app()->getLocale()); |
476 | | - })->get()->pluck('name', 'id')->filter()->toArray(); |
| 500 | + $currentLang = app()->getLocale(); |
| 501 | + |
| 502 | + $defaultLocale = null; |
| 503 | + if (class_exists(\Moox\Localization\Models\Localization::class)) { |
| 504 | + $localization = \Moox\Localization\Models\Localization::where('is_default', true) |
| 505 | + ->where('is_active_admin', true) |
| 506 | + ->with('language') |
| 507 | + ->first(); |
| 508 | + |
| 509 | + if ($localization && $localization->language) { |
| 510 | + $defaultLocale = $localization->locale_variant ?: $localization->language->alpha2; |
| 511 | + } |
| 512 | + } |
| 513 | + |
| 514 | + $collectionOptions = MediaCollection::with('translations') |
| 515 | + ->get() |
| 516 | + ->mapWithKeys(function ($item) use ($currentLang, $defaultLocale) { |
| 517 | + $name = |
| 518 | + $item->translate($currentLang)?->name |
| 519 | + ?? ($defaultLocale ? $item->translate($defaultLocale)?->name : null) |
| 520 | + ?? $item->translations->first()?->name |
| 521 | + ?? ('ID: ' . $item->id); |
| 522 | + |
| 523 | + return [$item->id => $name]; |
| 524 | + }) |
| 525 | + ->toArray(); |
477 | 526 |
|
478 | 527 | $uploaderOptions = []; |
479 | 528 | $uploaderTypes = Media::query() |
|
0 commit comments