Skip to content

Commit 53b8b92

Browse files
committed
Use of Laravel Linkable
1 parent 1f29e16 commit 53b8b92

File tree

5 files changed

+36
-88
lines changed

5 files changed

+36
-88
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"php": ">=8.2",
1919
"illuminate/support": "^10.0|^11.0",
2020
"laravel/nova": "^4.0",
21+
"novius/laravel-linkable": "^1.0",
2122
"novius/laravel-meta": "^1.0",
2223
"novius/laravel-nova-field-preview": "^2.0",
2324
"novius/laravel-nova-publishable": "^3.0",

lang/en/page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
return [
4+
'linkableGroup' => 'Pages',
5+
46
'title' => 'Title',
57
'slug' => 'Slug',
68
'template' => 'Template',

lang/fr/page.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22

33
return [
4+
'linkableGroup' => 'Pages',
5+
46
'title' => 'Titre',
57
'slug' => 'Slug',
68
'template' => 'Gabarit',

src/LaravelNovaPageManagerServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Illuminate\Support\Facades\Validator;
66
use Illuminate\Support\ServiceProvider;
77
use Laravel\Nova\Nova;
8+
use Novius\LaravelLinkable\Facades\Linkable;
89
use Novius\LaravelNovaPageManager\Console\FrontControllerCommand;
910
use Novius\LaravelNovaPageManager\Models\Page;
1011

@@ -21,6 +22,10 @@ public function boot(): void
2122
Nova::resources(config('laravel-nova-page-manager.resources', []));
2223
});
2324

25+
$this->app->booted(function () {
26+
Linkable::addModels([Page::class]);
27+
});
28+
2429
$packageDir = dirname(__DIR__);
2530

2631
$this->publishes([$packageDir.'/config' => config_path()], 'config');

src/Models/Page.php

Lines changed: 26 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,15 @@
1010
use Illuminate\Database\Eloquent\Relations\HasMany;
1111
use Illuminate\Support\Arr;
1212
use Illuminate\Support\Carbon;
13-
use Illuminate\Support\Facades\Auth;
14-
use Illuminate\Support\Facades\Route;
1513
use Illuminate\Support\Str;
14+
use Novius\LaravelLinkable\Configs\LinkableConfig;
15+
use Novius\LaravelLinkable\Traits\Linkable;
1616
use Novius\LaravelMeta\Enums\IndexFollow;
1717
use Novius\LaravelMeta\MetaModelConfig;
1818
use Novius\LaravelMeta\Traits\HasMeta;
1919
use Novius\LaravelPublishable\Enums\PublicationStatus;
2020
use Novius\LaravelPublishable\Traits\Publishable;
2121
use Novius\LaravelTranslatable\Traits\Translatable;
22-
use Psr\Container\ContainerExceptionInterface;
23-
use Psr\Container\NotFoundExceptionInterface;
2422
use RuntimeException;
2523
use Spatie\Sluggable\HasSlug;
2624
use Spatie\Sluggable\SlugOptions;
@@ -69,6 +67,7 @@ class Page extends Model
6967
{
7068
use HasMeta;
7169
use HasSlug;
70+
use Linkable;
7271
use Publishable;
7372
use Translatable;
7473

@@ -111,95 +110,11 @@ public function children(): HasMany
111110
return $this->hasMany(static::class, 'parent_id', 'id');
112111
}
113112

114-
public function url(): ?string
115-
{
116-
$routeName = config('laravel-nova-page-manager.front_route_name');
117-
$parameter = $this->getUrlParameter();
118-
119-
if ($routeName === null || ! $this->exists || ! $parameter) {
120-
return null;
121-
}
122-
123-
return route($routeName, [
124-
$parameter => $this->slug,
125-
]);
126-
}
127-
128-
public function previewUrl(): ?string
129-
{
130-
$routeName = config('laravel-nova-page-manager.front_route_name');
131-
$parameter = $this->getUrlParameter();
132-
133-
if ($routeName === null || ! $this->exists || ! $parameter) {
134-
return null;
135-
}
136-
137-
$params = [
138-
$parameter => $this->slug,
139-
];
140-
141-
$guard = config('laravel-nova-page-manager.guard_preview');
142-
if (empty($guard) && ! $this->isPublished()) {
143-
$params['previewToken'] = $this->preview_token;
144-
}
145-
146-
return route($routeName, $params);
147-
}
148-
149-
protected function getUrlParameter(): ?string
150-
{
151-
$parameter = config('laravel-nova-page-manager.front_route_parameter');
152-
153-
if (! empty($parameter)) {
154-
return $parameter;
155-
}
156-
157-
$routeName = config('laravel-nova-page-manager.front_route_name');
158-
if (empty($routeName)) {
159-
return null;
160-
}
161-
162-
$route = Route::getRoutes()->getByName($routeName);
163-
if (! $route) {
164-
return null;
165-
}
166-
167-
if (! preg_match('/({\w+})/', $route->uri(), $matches)) {
168-
return null;
169-
}
170-
171-
return substr($matches[0], 1, -1);
172-
}
173-
174113
public function getRouteKeyName(): string
175114
{
176115
return 'slug';
177116
}
178117

179-
public function resolveRouteBinding($value, $field = null)
180-
{
181-
$guard = config('laravel-nova-page-manager.guard_preview');
182-
$query = static::where('locale', app()->currentLocale());
183-
184-
if (! empty($guard) && Auth::guard($guard)->check()) {
185-
return $this->resolveRouteBindingQuery($query, $value, $field)->first();
186-
}
187-
188-
if (request()->has('previewToken')) {
189-
$query->where(/**
190-
* @throws ContainerExceptionInterface
191-
* @throws NotFoundExceptionInterface
192-
*/ function (Builder $query) {
193-
$query->published()
194-
->orWhere('preview_token', request()->get('previewToken'));
195-
});
196-
197-
return $this->resolveRouteBindingQuery($query, $value, $field)->first();
198-
}
199-
200-
return $this->resolveRouteBindingQuery($query->published(), $value, $field)->first();
201-
}
202-
203118
public function getSlugOptions(): SlugOptions
204119
{
205120
return SlugOptions::create()
@@ -221,6 +136,29 @@ public function getMetaConfig(): MetaModelConfig
221136
return $this->metaConfig;
222137
}
223138

139+
protected LinkableConfig $_linkableConfig;
140+
141+
public function linkableConfig(): LinkableConfig
142+
{
143+
if (! isset($this->_linkableConfig)) {
144+
$this->_linkableConfig = new LinkableConfig(
145+
routeName: config('laravel-nova-page-manager.front_route_name'),
146+
routeParameterName: config('laravel-nova-page-manager.front_route_parameter'),
147+
optionLabel: 'title',
148+
optionGroup: trans('laravel-nova-page-manager::page.linkableGroup'),
149+
resolveQuery: function (Builder|Page $query) {
150+
$query->where('locale', app()->currentLocale());
151+
},
152+
resolveNotPreviewQuery: function (Builder|Page $query) {
153+
$query->published();
154+
},
155+
previewTokenField: 'preview_token'
156+
);
157+
}
158+
159+
return $this->_linkableConfig;
160+
}
161+
224162
protected function seoCanonicalUrl(): Attribute
225163
{
226164
return Attribute::make(

0 commit comments

Comments
 (0)