Skip to content

Commit b2f02b0

Browse files
committed
fix(media): fix collection name display and synchronization - Display translated collection names in dropdowns instead of IDs - Automatically sync collection_name when collection changes - Use default language for collection_name consistency - Fix navigation badge count display
1 parent 09a7789 commit b2f02b0

File tree

4 files changed

+170
-42
lines changed

4 files changed

+170
-42
lines changed

packages/media/src/Models/Media.php

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace Moox\Media\Models;
44

5-
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
6-
use Astrotomic\Translatable\Translatable;
75
use Exception;
8-
use Illuminate\Database\Eloquent\Relations\MorphTo;
9-
use Illuminate\Support\Facades\DB;
10-
use Moox\Media\Traits\HasMediaUsable;
116
use Spatie\Image\Enums\Fit;
127
use Spatie\MediaLibrary\HasMedia;
8+
use Illuminate\Support\Facades\DB;
9+
use Moox\Media\Traits\HasMediaUsable;
10+
use Astrotomic\Translatable\Translatable;
11+
use Moox\Localization\Models\Localization;
1312
use Spatie\MediaLibrary\InteractsWithMedia;
13+
use Illuminate\Database\Eloquent\Relations\MorphTo;
1414
use Spatie\MediaLibrary\MediaCollections\Models\Media as BaseMedia;
15+
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
1516

1617
class Media extends BaseMedia implements HasMedia, TranslatableContract
1718
{
@@ -144,8 +145,56 @@ protected static function booted()
144145

145146
static::saving(function ($media) {
146147
if ($media->media_collection_id) {
147-
$collection = MediaCollection::find($media->media_collection_id);
148-
$media->collection_name = $collection?->name ?? null;
148+
$collectionChanged = false;
149+
if ($media->exists) {
150+
if ($media->isDirty('media_collection_id')) {
151+
$originalCollectionId = $media->getOriginal('media_collection_id');
152+
$newCollectionId = $media->media_collection_id;
153+
$collectionChanged = $originalCollectionId != $newCollectionId;
154+
}
155+
} else {
156+
$collectionChanged = true;
157+
}
158+
159+
if (!$media->exists || $collectionChanged || empty($media->collection_name)) {
160+
$collection = MediaCollection::with('translations')->find($media->media_collection_id);
161+
162+
if ($collection) {
163+
$defaultLocale = config('app.locale');
164+
165+
if (class_exists(Localization::class)) {
166+
$localization = Localization::where('is_default', true)
167+
->where('is_active_admin', true)
168+
->with('language')
169+
->first();
170+
171+
if ($localization && $localization->language) {
172+
$defaultLocale = $localization->locale_variant ?: $localization->language->alpha2;
173+
}
174+
}
175+
176+
$translation = $collection->translations->firstWhere('locale', $defaultLocale);
177+
$newCollectionName = null;
178+
179+
if ($translation && !empty($translation->name)) {
180+
$newCollectionName = $translation->name;
181+
} else {
182+
if ($collection->translations->isNotEmpty()) {
183+
$newCollectionName = $collection->translations->first()->name;
184+
} else {
185+
$newCollectionName = $collection->name ?? null;
186+
}
187+
}
188+
189+
if ($collectionChanged || !empty($newCollectionName)) {
190+
$media->collection_name = $newCollectionName;
191+
}
192+
} else {
193+
$media->collection_name = null;
194+
}
195+
}
196+
} elseif ($media->isDirty('media_collection_id') && !$media->media_collection_id) {
197+
$media->collection_name = null;
149198
}
150199
});
151200
}

packages/media/src/Resources/MediaCollectionResource.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ public static function getPages(): array
216216

217217
public static function getNavigationBadge(): ?string
218218
{
219-
return static::getModel()::whereHas('translations', function ($query) {
220-
$query->where('locale', app()->getLocale());
221-
})->count();
219+
return static::getModel()::count();
222220
}
223221
}

packages/media/src/Resources/MediaResource.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,15 +336,51 @@ public static function form(Schema $schema): Schema
336336
Select::make('media_collection_id')
337337
->label(__('media::fields.collection'))
338338
->disabled(fn ($record) => $record?->getOriginal('write_protected'))
339-
->options(
340-
MediaCollection::whereHas('translations', function ($query) {
341-
$query->where('locale', app()->getLocale());
342-
})->get()->pluck('name', 'id')->filter()->toArray()
343-
)
339+
->options(function ($record, $livewire) {
340+
$currentLang = $livewire->lang ?? app()->getLocale();
341+
342+
$collections = MediaCollection::query()
343+
->with('translations')
344+
->get();
345+
346+
$options = [];
347+
foreach ($collections as $collection) {
348+
$name = null;
349+
350+
$translation = $collection->translations()->where('locale', $currentLang)->first();
351+
352+
if ($translation && !empty($translation->name)) {
353+
$name = $translation->name;
354+
} else {
355+
if (class_exists(\Moox\Localization\Models\Localization::class)) {
356+
$defaultLocale = optional(\Moox\Localization\Models\Localization::where('is_default', true)
357+
->first()?->language)->alpha2 ?? config('app.locale');
358+
359+
$translation = $collection->translations()->where('locale', $defaultLocale)->first();
360+
if ($translation && !empty($translation->name)) {
361+
$name = $translation->name;
362+
}
363+
}
364+
365+
if (empty($name)) {
366+
$anyTranslation = $collection->translations()->whereNotNull('name')->first();
367+
$name = $anyTranslation?->name ?? 'Collection #' . $collection->id;
368+
}
369+
}
370+
371+
$options[$collection->id] = $name;
372+
}
373+
374+
return $options;
375+
})
344376
->default(fn ($record) => $record->media_collection_id)
345377
->afterStateUpdated(function ($state, $record) {
346378
if ($state !== $record->media_collection_id) {
347379
$record->media_collection_id = $state;
380+
381+
382+
$record->collection_name = null;
383+
348384
$record->save();
349385
}
350386
}),

packages/media/src/Resources/MediaResource/Pages/ListMedia.php

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -127,31 +127,51 @@ public function getHeaderActions(): array
127127
->schema([
128128
Select::make('media_collection_id')
129129
->label(__('media::fields.collection'))
130-
->options(fn () => (function () {
131-
$defaultLang = Localization::where('is_default', true)
132-
->value('language_id');
133-
134-
$alpha2 = $defaultLang
135-
? StaticLanguage::whereKey($defaultLang)->value('alpha2')
136-
: config('app.locale');
137-
138-
return MediaCollection::query()
139-
->with(['translations' => fn ($q) => $q->where('locale', $alpha2)])
140-
->whereHas('translations', fn ($q) => $q->where('locale', $alpha2))
141-
->get()
142-
->mapWithKeys(function ($item) use ($alpha2) {
143-
$translation = method_exists($item, 'translate')
144-
? $item->translate($alpha2)
145-
: ($item->translations->first() ?? null);
146-
147-
$name = $translation?->name ?? $item->name;
148-
149-
return [$item->id => $name];
150-
})
151-
->filter()
152-
->toArray();
153-
})())
154-
->default(MediaCollection::first()->id)
130+
->options(function () {
131+
$currentLang = $this->lang ?? app()->getLocale();
132+
133+
$collections = MediaCollection::query()
134+
->with('translations')
135+
->get();
136+
137+
$options = [];
138+
foreach ($collections as $collection) {
139+
$translation = $collection->translations()->where('locale', $currentLang)->first();
140+
141+
if ($translation && !empty($translation->name)) {
142+
$name = $translation->name;
143+
} else {
144+
if (class_exists(Localization::class)) {
145+
$defaultLocale = Localization::where('is_default', true)
146+
->where('is_active_admin', true)
147+
->with('language')
148+
->first();
149+
150+
if ($defaultLocale && $defaultLocale->language) {
151+
$defaultLang = $defaultLocale->locale_variant ?: $defaultLocale->language->alpha2;
152+
$fallbackTranslation = $collection->translations()->where('locale', $defaultLang)->first();
153+
if ($fallbackTranslation && !empty($fallbackTranslation->name)) {
154+
$name = $fallbackTranslation->name;
155+
}
156+
}
157+
}
158+
159+
if (empty($name)) {
160+
$anyTranslation = $collection->translations()->whereNotNull('name')->first();
161+
$name = $anyTranslation?->name;
162+
}
163+
164+
if (empty($name)) {
165+
$name = __('media::fields.uncategorized');
166+
}
167+
}
168+
169+
$options[$collection->id] = trim((string) $name);
170+
}
171+
172+
return array_filter($options, fn($value) => !empty($value));
173+
})
174+
->default(MediaCollection::first()?->id)
155175
->searchable()
156176
->required()
157177
->live(),
@@ -188,8 +208,32 @@ public function getHeaderActions(): array
188208
}
189209

190210
$collectionId = $get('media_collection_id');
191-
$collection = MediaCollection::find($collectionId);
192-
$collectionName = $collection?->name ?? __('media::fields.uncategorized');
211+
$collection = MediaCollection::with('translations')->find($collectionId);
212+
213+
$collectionName = __('media::fields.uncategorized');
214+
if ($collection) {
215+
$defaultLang = config('app.locale');
216+
217+
$localization = Localization::where('is_default', true)
218+
->where('is_active_admin', true)
219+
->with('language')
220+
->first();
221+
222+
if ($localization && $localization->language) {
223+
$defaultLang = $localization->locale_variant ?: $localization->language->alpha2;
224+
}
225+
226+
$translation = $collection->translations->firstWhere('locale', $defaultLang);
227+
228+
if ($translation && !empty($translation->name)) {
229+
$collectionName = $translation->name;
230+
} elseif ($collection->translations->isNotEmpty()) {
231+
$collectionName = $collection->translations->first()->name;
232+
} elseif (method_exists($collection, 'translate')) {
233+
$translation = $collection->translate($defaultLang);
234+
$collectionName = $translation?->name ?? __('media::fields.uncategorized');
235+
}
236+
}
193237

194238
$defaultLang = optional(Localization::where('is_default', true)
195239
->first()?->language)->alpha2 ?? config('app.locale');
@@ -232,6 +276,7 @@ public function getHeaderActions(): array
232276
$media = $fileAdder->preservingOriginal()->toMediaCollection($collectionName);
233277

234278
$media->media_collection_id = $collectionId;
279+
$media->collection_name = $collectionName;
235280
$media->save();
236281

237282
$title = pathinfo($tempFile->getClientOriginalName(), PATHINFO_FILENAME);

0 commit comments

Comments
 (0)