Breadcrumbs: always start at Home, full category trail, BreadcrumbList JSON-LD
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9d7f92ebfc
commit
fb239a97cb
8 files changed
+114
-18
No files matched your search
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Catalog;
|
||||
|
||||
use Modules\Core\Catalog\Services\CollectionService;
|
||||
|
||||
/**
|
||||
* Breadcrumb items for a collection and its ancestors, root first, in the
|
||||
* shape <x-breadcrumb> takes. Home is added by the component itself.
|
||||
*
|
||||
* Interim: the indexed `ancestors` field gives the right ids and order, but
|
||||
* its names are translated once at index time (CollectionIndexer uses
|
||||
* translateAttribute('name') with no locale), so they'd show in the wrong
|
||||
* language on the other locale. Each ancestor is re-read via getById() for a
|
||||
* current-locale name instead. Trees are shallow, so it's one or two extra
|
||||
* index lookups. Drop the re-read once core indexes ancestor names per locale.
|
||||
*/
|
||||
final class CollectionTrail
|
||||
{
|
||||
public function __construct(
|
||||
private readonly CollectionService $collections,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $collection a localized CollectionService document
|
||||
* @param bool $linkLast false when the collection itself is the current page
|
||||
* @return array<int, array{label: string, href?: string}>
|
||||
*/
|
||||
public function for(array $collection, bool $linkLast = true): array
|
||||
{
|
||||
$ancestors = collect($collection['ancestors'] ?? [])
|
||||
->map(fn (array $ancestor) => [
|
||||
// Untranslated collections resolve to a null name, so keep the
|
||||
// index-time name as a fallback.
|
||||
'label' => $this->collections->getById((int) $ancestor['id'])['name'] ?? $ancestor['name'],
|
||||
'href' => route('category.show', ['id' => $ancestor['id']]),
|
||||
]);
|
||||
|
||||
$self = ['label' => $collection['name'] ?? ''];
|
||||
|
||||
if ($linkLast) {
|
||||
$self['href'] = route('category.show', ['id' => $collection['id']]);
|
||||
}
|
||||
|
||||
return [...$ancestors->values()->all(), $self];
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Catalog\CollectionTrail;
|
||||
use App\Catalog\ProductListing;
|
||||
use App\Catalog\ProductListingPage;
|
||||
use Illuminate\Http\Response;
|
||||
@@ -12,6 +13,7 @@ class CategoryController extends Controller
|
||||
public function __construct(
|
||||
private readonly ProductListingPage $listingPage,
|
||||
private readonly CollectionService $collections,
|
||||
private readonly CollectionTrail $trail,
|
||||
) {}
|
||||
|
||||
public function show(string $locale, int $collection)
|
||||
@@ -27,6 +29,10 @@ public function show(string $locale, int $collection)
|
||||
$collectionData['id'],
|
||||
);
|
||||
|
||||
return view('category.show', [...$data, 'collection' => $collectionData]);
|
||||
return view('category.show', [
|
||||
...$data,
|
||||
'collection' => $collectionData,
|
||||
'breadcrumb' => $this->trail->for($collectionData, linkLast: false),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Catalog\CollectionTrail;
|
||||
use App\Catalog\ProductListing;
|
||||
use App\Catalog\ProductListingPage;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
@@ -11,6 +12,7 @@
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Lunar\Models\Product;
|
||||
use Lunar\Models\ProductVariant;
|
||||
use Modules\Core\Catalog\Services\CollectionService;
|
||||
use Modules\Core\Catalog\Services\ProductService;
|
||||
use Modules\Core\Review\Models\ProductReview;
|
||||
|
||||
@@ -19,6 +21,8 @@ class ProductController extends Controller
|
||||
public function __construct(
|
||||
private readonly ProductService $products,
|
||||
private readonly ProductListingPage $listingPage,
|
||||
private readonly CollectionService $collections,
|
||||
private readonly CollectionTrail $trail,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -44,18 +48,40 @@ public function show(string $locale, int $id)
|
||||
|
||||
$product = $this->mergeJustSubmittedReview($product);
|
||||
|
||||
$collection = $product['collections'][0] ?? null;
|
||||
// dd($product);
|
||||
[$productOptions, $variantsData] = $this->buildOptionPicker($id);
|
||||
|
||||
return view('product.show', [
|
||||
'collection' => $collection,
|
||||
'breadcrumb' => [...$this->collectionTrail($product), ['label' => $product['name']]],
|
||||
'product' => $product,
|
||||
'productOptions' => $productOptions,
|
||||
'variantsData' => $variantsData,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The product's first directly-assigned collection with its ancestors, or
|
||||
* the all-products page when it has none. The product document's own
|
||||
* `collections` entries carry neither ancestors nor a current-locale name,
|
||||
* so the collection is re-read from the collection index.
|
||||
*
|
||||
* @return array<int, array{label: string, href?: string}>
|
||||
*/
|
||||
private function collectionTrail(array $product): array
|
||||
{
|
||||
$assigned = $product['collections'][0] ?? null;
|
||||
$collection = $assigned !== null ? $this->collections->getById((int) $assigned['id']) : null;
|
||||
|
||||
if ($collection === null) {
|
||||
return [['label' => __('storefront.nav.products'), 'href' => route('products')]];
|
||||
}
|
||||
|
||||
// Untranslated collections resolve to a null name; the product's own
|
||||
// index-time name is the fallback, same as CollectionTrail's ancestors.
|
||||
$collection['name'] ??= $assigned['name'];
|
||||
|
||||
return $this->trail->for($collection);
|
||||
}
|
||||
|
||||
/**
|
||||
* One button/swatch group per product option (a product can have several
|
||||
* — e.g. a custom-photo product with size + style + person-count, each
|
||||
|
||||
@@ -20,10 +20,7 @@
|
||||
<div class="flex items-end justify-between gap-x-6 gap-y-3 flex-wrap mb-10 lg:mb-16">
|
||||
<h1 class="font-medium text-h2">{{ $collection['name'] }}</h1>
|
||||
|
||||
<x-breadcrumb :items="[
|
||||
['label' => __('storefront.nav.home'), 'href' => route('home')],
|
||||
['label' => $collection['name']],
|
||||
]" />
|
||||
<x-breadcrumb :items="$breadcrumb" />
|
||||
</div>
|
||||
|
||||
{{-- Everything that reflects sort / filter / page state lives in this
|
||||
|
||||
@@ -2,15 +2,42 @@
|
||||
|
||||
{{--
|
||||
$items: array of ['label' => string, 'href' => string|null]
|
||||
Home is always prepended here, so pages pass only their own trail.
|
||||
Last item is the current page — no link, no href needed.
|
||||
|
||||
Example:
|
||||
<x-breadcrumb :items="[
|
||||
['label' => 'Jackets', 'href' => '/category/jackets'],
|
||||
['label' => 'Jackets', 'href' => route('category.show', ['id' => 12])],
|
||||
['label' => 'Leather Jacket'],
|
||||
]" />
|
||||
|
||||
Also pushes matching BreadcrumbList JSON-LD to the layout's `seo` stack.
|
||||
--}}
|
||||
|
||||
@php
|
||||
// Untranslated collection names can arrive empty; skip them rather than
|
||||
// render an empty crumb.
|
||||
$items = array_values(array_filter([
|
||||
['label' => __('storefront.nav.home'), 'href' => route('home')],
|
||||
...$items,
|
||||
], fn ($item) => filled($item['label'] ?? null)));
|
||||
@endphp
|
||||
|
||||
@push('seo')
|
||||
<script type="application/ld+json">
|
||||
{!! json_encode([
|
||||
'@@context' => 'https://schema.org', {{-- @@ escapes Blade's @context directive --}}
|
||||
'@type' => 'BreadcrumbList',
|
||||
'itemListElement' => collect($items)->map(fn ($item, $index) => array_filter([
|
||||
'@type' => 'ListItem',
|
||||
'position' => $index + 1,
|
||||
'name' => $item['label'],
|
||||
'item' => $item['href'] ?? null,
|
||||
], fn ($value) => $value !== null))->values()->all(),
|
||||
], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG) !!}
|
||||
</script>
|
||||
@endpush
|
||||
|
||||
<nav aria-label="Breadcrumb">
|
||||
<ol {{ $attributes->merge(['class' => 'flex items-center flex-wrap gap-1 text-base text-black']) }}>
|
||||
@foreach ($items as $index => $item)
|
||||
|
||||
@@ -6,12 +6,7 @@
|
||||
@section('content')
|
||||
<div class="max-w-7xl mx-auto px-5 sm:px-8 py-12">
|
||||
|
||||
<x-breadcrumb class="mb-8 justify-end lg:mb-14" :items="[
|
||||
$collection
|
||||
? ['label' => $collection['name'], 'href' => route('category.show', ['id' => $collection['id']])]
|
||||
: ['label' => __('storefront.nav.products'), 'href' => route('products')],
|
||||
['label' => $product['name']],
|
||||
]" />
|
||||
<x-breadcrumb class="mb-8 justify-end lg:mb-14" :items="$breadcrumb" />
|
||||
|
||||
<div
|
||||
class="grid grid-cols-1 md:grid-cols-2 gap-12"
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
<h1 class="font-medium text-h2">{{ __('storefront.shop.all_products') }}</h1>
|
||||
|
||||
<x-breadcrumb :items="[
|
||||
['label' => __('storefront.nav.home'), 'href' => route('home')],
|
||||
['label' => __('storefront.shop.all_products')],
|
||||
]" />
|
||||
</div>
|
||||
|
||||
@@ -19,8 +19,7 @@
|
||||
<h1 class="font-medium text-h2">{{ $heading }}</h1>
|
||||
|
||||
<x-breadcrumb :items="[
|
||||
['label' => __('storefront.nav.home'), 'href' => route('home')],
|
||||
['label' => __('storefront.search.results_for') . $heading],
|
||||
['label' => __('storefront.search.results_for') . ' ' . $heading],
|
||||
]" />
|
||||
</div>
|
||||
|
||||
|
||||
Reference in new issue
Block a user