|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Nova; |
| 4 | + |
| 5 | +use App\Models\MenuItem as MenuItemModel; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use Laravel\Nova\Fields\BelongsTo; |
| 8 | +use Laravel\Nova\Fields\Boolean; |
| 9 | +use Laravel\Nova\Fields\Code; |
| 10 | +use Laravel\Nova\Fields\ID; |
| 11 | +use Laravel\Nova\Fields\KeyValue; |
| 12 | +use Laravel\Nova\Fields\Text; |
| 13 | +use Laravel\Nova\Http\Requests\NovaRequest; |
| 14 | +use Outl1ne\NovaSortable\Traits\HasSortableRows; |
| 15 | + |
| 16 | +class MenuItem extends Resource |
| 17 | +{ |
| 18 | + use HasSortableRows; |
| 19 | + |
| 20 | + public static $model = MenuItemModel::class; |
| 21 | + |
| 22 | + public static $title = 'id'; |
| 23 | + |
| 24 | + public static $search = [ |
| 25 | + 'id', |
| 26 | + 'label', |
| 27 | + 'label_key', |
| 28 | + 'url', |
| 29 | + 'route_name', |
| 30 | + ]; |
| 31 | + |
| 32 | + public static $displayInNavigation = false; |
| 33 | + |
| 34 | + public function title(): string |
| 35 | + { |
| 36 | + $label = $this->resource?->resolvedLabel() ?: "Item #{$this->id}"; |
| 37 | + $href = $this->resource?->resolvedHref(); |
| 38 | + $hrefPart = $href ? " · {$href}" : ''; |
| 39 | + |
| 40 | + return $label.$hrefPart; |
| 41 | + } |
| 42 | + |
| 43 | + public function fields(Request $request): array |
| 44 | + { |
| 45 | + return [ |
| 46 | + ID::make()->sortable(), |
| 47 | + |
| 48 | + BelongsTo::make('Section', 'section', MenuSection::class) |
| 49 | + ->sortable() |
| 50 | + ->rules('required'), |
| 51 | + |
| 52 | + Text::make('Label Key', 'label_key') |
| 53 | + ->nullable() |
| 54 | + ->rules('nullable', 'max:255') |
| 55 | + ->help('Preferred. Example: menu.webinars'), |
| 56 | + |
| 57 | + Text::make('Label (literal)', 'label') |
| 58 | + ->nullable() |
| 59 | + ->rules('nullable', 'max:255') |
| 60 | + ->help('Used only if Label Key is empty (and no locale override exists).'), |
| 61 | + |
| 62 | + KeyValue::make('Label Overrides', 'label_overrides') |
| 63 | + ->keyLabel('Locale') |
| 64 | + ->valueLabel('Label') |
| 65 | + ->nullable() |
| 66 | + ->rules('nullable') |
| 67 | + ->help('Optional per-locale labels. Example key: en, fr, de'), |
| 68 | + |
| 69 | + Text::make('Route Name', 'route_name') |
| 70 | + ->nullable() |
| 71 | + ->rules('nullable', 'max:255') |
| 72 | + ->help('Preferred for internal links, e.g. educational-resources'), |
| 73 | + |
| 74 | + Code::make('Route Params', 'route_params') |
| 75 | + ->json() |
| 76 | + ->nullable() |
| 77 | + ->rules('nullable') |
| 78 | + ->help('Optional JSON object, e.g. {"slug":"xyz"}'), |
| 79 | + |
| 80 | + Text::make('URL', 'url') |
| 81 | + ->nullable() |
| 82 | + ->rules('nullable', 'max:2048') |
| 83 | + ->help('Use for external links or hardcoded internal paths like /podcasts'), |
| 84 | + |
| 85 | + Boolean::make('Open in new tab', 'open_in_new_tab')->default(false), |
| 86 | + Boolean::make('Active', 'is_active')->default(true), |
| 87 | + |
| 88 | + Text::make('Preview', function () { |
| 89 | + $label = $this->resource?->resolvedLabel() ?: ''; |
| 90 | + $href = $this->resource?->resolvedHref() ?: ''; |
| 91 | + return trim($label.' '.$href); |
| 92 | + })->onlyOnDetail(), |
| 93 | + ]; |
| 94 | + } |
| 95 | + |
| 96 | + public static function indexQuery(NovaRequest $request, $query) |
| 97 | + { |
| 98 | + return $query->orderBy('sort_order')->orderBy('id'); |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments