Skip to content

Commit c1931ea

Browse files
committed
Add JsonCasted on extras and casts method on PageTemplate
1 parent 53b8b92 commit c1931ea

File tree

6 files changed

+40
-3
lines changed

6 files changed

+40
-3
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ In `app/Nova/Templates/StandardTemplate.php`
9494

9595
namespace App\Nova\Templates;
9696

97+
use Laravel\Nova\Fields\Date;
9798
use Laravel\Nova\Fields\Trix;
9899
use Novius\LaravelNovaPageManager\Templates\AbstractPageTemplate;
99100

@@ -112,12 +113,31 @@ class StandardTemplate extends AbstractPageTemplate
112113
public function fields(): array
113114
{
114115
return [
115-
Trix::make(trans('laravel-nova-page-manager::template.field_content'), 'content'),
116+
Trix::make('Content', 'content'),
117+
Date::make('Date', 'date'),
118+
];
119+
}
120+
121+
public function casts() : array
122+
{
123+
return [
124+
'date' => 'date',
116125
];
117126
}
118127
}
119128
```
120129

130+
Pour utiliser les champs spécifique du template :
131+
132+
```php
133+
$page = \Novius\LaravelNovaPageManager\Models\Page::where('template', 'standard')->first();
134+
135+
$content = $page->extras['content'];
136+
137+
// Date will be a Carbon instance, thanks to the cast
138+
$date = $page->extras['date'];
139+
```
140+
121141
## Lint
122142

123143
Run php-cs with:

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"illuminate/support": "^10.0|^11.0",
2020
"laravel/nova": "^4.0",
2121
"novius/laravel-linkable": "^1.0",
22+
"novius/laravel-json-casted": "^1.0.0",
2223
"novius/laravel-meta": "^1.0",
2324
"novius/laravel-nova-field-preview": "^2.0",
2425
"novius/laravel-nova-publishable": "^3.0",

src/Contracts/PageTemplate.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,6 @@ public function templateUniqueKey(): string;
99
public function templateName(): string;
1010

1111
public function fields(): array;
12+
13+
public function casts(): array;
1214
}

src/Helpers/TemplatesHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function getTemplates(?Resource $resource = null): Collection
2828
})->filter();
2929
}
3030

31-
public static function getTemplate(string $templateKey, Resource $resource): ?AbstractPageTemplate
31+
public static function getTemplate(string $templateKey, ?Resource $resource = null): ?AbstractPageTemplate
3232
{
3333
$template = static::getTemplates($resource)->firstWhere('templateKey', $templateKey);
3434
if (empty($template)) {

src/Models/Page.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
use Illuminate\Support\Arr;
1212
use Illuminate\Support\Carbon;
1313
use Illuminate\Support\Str;
14+
use Novius\LaravelJsonCasted\Casts\JsonWithCasts;
1415
use Novius\LaravelLinkable\Configs\LinkableConfig;
1516
use Novius\LaravelLinkable\Traits\Linkable;
1617
use Novius\LaravelMeta\Enums\IndexFollow;
1718
use Novius\LaravelMeta\MetaModelConfig;
1819
use Novius\LaravelMeta\Traits\HasMeta;
20+
use Novius\LaravelNovaPageManager\Helpers\TemplatesHelper;
1921
use Novius\LaravelPublishable\Enums\PublicationStatus;
2022
use Novius\LaravelPublishable\Traits\Publishable;
2123
use Novius\LaravelTranslatable\Traits\Translatable;
@@ -76,7 +78,7 @@ class Page extends Model
7678
protected $guarded = ['id'];
7779

7880
protected $casts = [
79-
'extras' => 'json',
81+
'extras' => JsonWithCasts::class.':getExtrasCasts',
8082
];
8183

8284
/**
@@ -159,6 +161,13 @@ public function linkableConfig(): LinkableConfig
159161
return $this->_linkableConfig;
160162
}
161163

164+
public function getExtrasCasts(): array
165+
{
166+
$template = $this->template ? TemplatesHelper::getTemplate($this->template) : null;
167+
168+
return $template?->casts() ?? [];
169+
}
170+
162171
protected function seoCanonicalUrl(): Attribute
163172
{
164173
return Attribute::make(

src/Templates/AbstractPageTemplate.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,9 @@ public function __construct(?Resource $resource = null)
1313
{
1414
$this->resource = $resource;
1515
}
16+
17+
public function casts(): array
18+
{
19+
return [];
20+
}
1621
}

0 commit comments

Comments
 (0)