storeWithDesign($request, $slug); $data = $request->validate([ 'after' => ['nullable', 'string', Rule::in(array_column(SectionEditor::homeSections($store->plan), 'id'))], ]); [$plan] = SectionEditor::addToHome($store->plan, $data['after'] ?? null, 'highlights', [ 'type' => 'bento', 'variant' => 'mosaic', 'content' => ['heading' => null, 'tiles' => $this->starterTiles($store)], ]); return $this->saveDesign($store, $validator, $plan, __('Bento tiles added. Change any tile below.')); } public function update(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse { $store = $this->storeWithDesign($request, $slug); $this->assertSection($store, $sectionId, 'bento'); $data = $request->validate([ 'heading' => ['nullable', 'string', 'max:80'], 'variant' => ['required', Rule::in(PlanSchema::SECTIONS['bento'])], 'tiles' => ['required', 'array', 'min:3', 'max:6'], 'tiles.*.title' => ['required', 'string', 'max:60'], 'tiles.*.text' => ['nullable', 'string', 'max:140'], 'tiles.*.href' => ['required', 'string', 'max:300', 'regex:'.PlanSchema::LINK_PATTERN], 'tiles.*.image' => ['nullable', 'string', 'regex:'.HeroImages::PATTERN], 'tiles.*.tone' => ['nullable', Rule::in(['primary', 'accent', 'dark', 'surface'])], ], [ 'tiles.min' => __('Keep at least 3 tiles.'), 'tiles.max' => __('Use at most 6 tiles.'), 'tiles.*.href.regex' => __('Use a link such as /shop, /collections/gift-sets, #about or https://...'), ]); $images = array_values(array_filter(array_column($data['tiles'], 'image'))); if (HeroImages::unknown($store, $images) !== []) { throw ValidationException::withMessages(['section' => __('Choose photos from your products or uploads.')]); } $tiles = array_map(fn (array $tile) => [ 'title' => trim($tile['title']), 'text' => filled($tile['text'] ?? null) ? trim($tile['text']) : null, 'href' => trim($tile['href']), 'image' => $tile['image'] ?? null, 'tone' => $tile['tone'] ?? 'surface', ], array_values($data['tiles'])); $content = ['heading' => filled($data['heading'] ?? null) ? trim($data['heading']) : null, 'tiles' => $tiles]; return $this->saveDesign($store, $validator, SectionEditor::update($store->plan, $sectionId, $content, $data['variant']), __('Bento tiles saved.')); } public function destroy(Request $request, PlanValidator $validator, string $slug, string $sectionId): RedirectResponse { $store = $this->storeWithDesign($request, $slug); $this->assertSection($store, $sectionId, 'bento'); return $this->saveDesign($store, $validator, SectionEditor::remove($store->plan, $sectionId), __('Bento tiles removed.')); } /** * Three tiles that work for any store: the whole range (with a product photo when there is one), * the first collection or the products section, and the brand story. * * @return list> */ private function starterTiles(Store $store): array { $photo = HeroImages::choices($store); $productPhoto = collect($photo)->first(fn (array $choice) => str_starts_with($choice['ref'], 'product:')); $collection = $store->collections()->first(); $sections = array_column(SectionEditor::homeSections($store->plan ?? []), 'id'); $about = collect(SectionEditor::ofType($store->plan ?? [], 'about'))->firstWhere('page_id', 'home'); $grid = collect(SectionEditor::ofType($store->plan ?? [], 'product_grid'))->firstWhere('page_id', 'home'); return [ ['title' => 'Shop the range', 'text' => null, 'href' => '/shop', 'image' => $productPhoto['ref'] ?? null, 'tone' => 'primary'], $collection !== null ? ['title' => $collection->name, 'text' => null, 'href' => '/collections/'.$collection->slug, 'image' => null, 'tone' => 'accent'] : ['title' => 'Bestsellers', 'text' => null, 'href' => $grid !== null && in_array($grid['section']['id'], $sections, true) ? '#'.$grid['section']['id'] : '/shop', 'image' => null, 'tone' => 'accent'], ['title' => 'Our story', 'text' => null, 'href' => $about !== null ? '#'.$about['section']['id'] : '/shop', 'image' => null, 'tone' => 'dark'], ]; } }