Feature: Resolving translated fields based on current locale, while also resturning a correct length aware paginator for the results
This commit is contained in:
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace Modules\Core\Catalog;
|
||||
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Contracts\Pagination\LengthAwarePaginator as LengthAwarePaginatorContract;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\App;
|
||||
use Lunar\Base\AttributeManifest;
|
||||
use Lunar\FieldTypes\TranslatedText;
|
||||
use Lunar\Models\Product;
|
||||
use Modules\Core\Localization\Services\LanguageCache;
|
||||
|
||||
@@ -19,12 +22,18 @@ use Modules\Core\Localization\Services\LanguageCache;
|
||||
*/
|
||||
class ProductService
|
||||
{
|
||||
public function __construct(private readonly LanguageCache $languages) {}
|
||||
public function __construct(
|
||||
private readonly LanguageCache $languages,
|
||||
private readonly AttributeManifest $attributes,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @return array{data: array<int, array>, meta: array}
|
||||
* Returns a real LengthAwarePaginator (not Scout's own paginateRaw() result -
|
||||
* see "Meilisearch driver quirk" below) so a controller/view gets normal
|
||||
* pagination behaviour ($products->links(), JSON serialization, etc.)
|
||||
* without ever touching the raw Meilisearch response directly.
|
||||
*/
|
||||
public function list(?ProductFilters $filters = null, int $perPage = 24, int $page = 1, ?ProductSort $sort = null): array
|
||||
public function list(?ProductFilters $filters = null, int $perPage = 24, int $page = 1, ?ProductSort $sort = null): LengthAwarePaginator
|
||||
{
|
||||
$options = ['filter' => $this->buildFilter($filters)];
|
||||
|
||||
@@ -36,17 +45,17 @@ class ProductService
|
||||
->options($options)
|
||||
->paginateRaw(perPage: $perPage, page: $page);
|
||||
|
||||
return [
|
||||
'data' => collect($this->hitsFrom($paginator))
|
||||
->map(fn (array $product) => $this->withLocalizedFields($product))
|
||||
->all(),
|
||||
'meta' => [
|
||||
'total' => $paginator->total(),
|
||||
'per_page' => $paginator->perPage(),
|
||||
'current_page' => $paginator->currentPage(),
|
||||
'last_page' => $paginator->lastPage(),
|
||||
],
|
||||
];
|
||||
$data = collect($this->hitsFrom($paginator))
|
||||
->map(fn (array $product) => $this->withLocalizedFields($product))
|
||||
->all();
|
||||
|
||||
return new LengthAwarePaginator(
|
||||
items: $data,
|
||||
total: $paginator->total(),
|
||||
perPage: $paginator->perPage(),
|
||||
currentPage: $paginator->currentPage(),
|
||||
options: ['path' => LengthAwarePaginator::resolveCurrentPath()],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,16 +89,20 @@ class ProductService
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the current-locale `name`/`description` from the indexer's
|
||||
* per-locale `name_{locale}`/`description_{locale}` fields, falling back to
|
||||
* the store's default language (LanguageCache::defaultLocale()) when the
|
||||
* current locale has no translation - e.g. a product with no English copy
|
||||
* yet still shows its Greek name/description on /en/ rather than rendering
|
||||
* blank. The raw per-locale keys are then stripped - every configured
|
||||
* locale's translation is indexed in Meilisearch (Lunar's base indexer
|
||||
* explodes every TranslatedText attribute into name_{locale}/
|
||||
* description_{locale} per store language), but once resolved into `name`/
|
||||
* `description`, callers only ever need the one that matched.
|
||||
* Resolves every translated Product attribute's current-locale value from the
|
||||
* indexer's per-locale `{handle}_{locale}` fields (e.g. `name_el`, `name_en`,
|
||||
* `seo_title_el`, ...) into a plain `{handle}` key, falling back to the store's
|
||||
* default language (LanguageCache::defaultLocale()) when the current locale
|
||||
* has no translation - e.g. a product with no English copy yet still shows its
|
||||
* Greek name on /en/ rather than rendering blank.
|
||||
*
|
||||
* Which handles are translated is read from AttributeManifest - the same
|
||||
* source Lunar's own ScoutIndexer reads when exploding a TranslatedText
|
||||
* attribute into `{handle}_{locale}` keys at index time - rather than a fixed
|
||||
* list, so a store's own custom translated attributes (e.g. `seo_title`) are
|
||||
* picked up automatically with no change here. The raw per-locale keys are
|
||||
* then stripped, since once resolved, callers only ever need the one that
|
||||
* matched the current locale.
|
||||
*
|
||||
* Deliberately not config('app.locale') - App::setLocale() overwrites that
|
||||
* config value on every request, so by request time it's just whatever the
|
||||
@@ -99,23 +112,36 @@ class ProductService
|
||||
{
|
||||
$locale = App::getLocale();
|
||||
$fallbackLocale = $this->languages->defaultLocale();
|
||||
$availableLocales = $this->languages->availableLocales();
|
||||
|
||||
$product['name'] = $product['name_'.$locale] ?? $product['name_'.$fallbackLocale] ?? null;
|
||||
$product['description'] = $product['description_'.$locale] ?? $product['description_'.$fallbackLocale] ?? null;
|
||||
foreach ($this->translatedAttributeHandles() as $handle) {
|
||||
$product[$handle] = $product[$handle.'_'.$locale] ?? $product[$handle.'_'.$fallbackLocale] ?? null;
|
||||
|
||||
foreach ($this->languages->availableLocales() as $availableLocale) {
|
||||
unset($product['name_'.$availableLocale], $product['description_'.$availableLocale]);
|
||||
foreach ($availableLocales as $availableLocale) {
|
||||
unset($product[$handle.'_'.$availableLocale]);
|
||||
}
|
||||
}
|
||||
|
||||
return $product;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, string>
|
||||
*/
|
||||
private function translatedAttributeHandles(): array
|
||||
{
|
||||
return $this->attributes->getSearchableAttributes((new Product)->getMorphClass())
|
||||
->filter(fn ($attribute) => $attribute->type === TranslatedText::class)
|
||||
->pluck('handle')
|
||||
->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* For the Meilisearch driver, Scout's paginateRaw() puts the whole raw response
|
||||
* (hits, query, processingTimeMs, ...) in items(), not a plain list of hits - the
|
||||
* actual documents are under the 'hits' key.
|
||||
*/
|
||||
private function hitsFrom(LengthAwarePaginator $paginator): array
|
||||
private function hitsFrom(LengthAwarePaginatorContract $paginator): array
|
||||
{
|
||||
$rawResponse = $paginator->items();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user