Skip to content

Commit d710471

Browse files
committed
feat: Implement preview functionality with DefaultPreviewProvider and PreviewFactory
1 parent f866002 commit d710471

12 files changed

Lines changed: 445 additions & 271 deletions

File tree

config/inspirecms.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,7 @@
421421
],
422422
],
423423
'segment_provider' => \SolutionForest\InspireCms\Content\DefaultSegmentProvider::class,
424+
'preview_provider' => \SolutionForest\InspireCms\Content\DefaultPreviewProvider::class,
424425
],
425426

426427
'sitemap' => [

resources/views/instructions/property-type-instructions.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@use('SolutionForest\InspireCms\Helpers\PropertyTypeHelper')
12
@php
23
use Illuminate\Support\Arr;
34
use SolutionForest\InspireCms\Dtos\PropertyTypeDto;
@@ -14,7 +15,7 @@
1415
1516
$translatable = $fieldType?->isTranslatable() ?? false;
1617
17-
$valueType = FieldTypeHelper::resolveFieldReturnType($fieldType);
18+
$valueType = PropertyTypeHelper::getFieldDisplayValueType($fieldType);
1819
1920
$propertyVarName = TemplateHelper::generatePropertyVarName($group, $field);
2021
if ($valueType != 'array' || $translatable) {

src/Base/Filament/Concerns/ContentPreviewEditorTrait.php

Lines changed: 29 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22

33
namespace SolutionForest\InspireCms\Base\Filament\Concerns;
44

5-
use Filament\Notifications\Notification;
65
use Filament\Resources\Pages\CreateRecord;
7-
use Filament\Support\Exceptions\Halt;
86
use Illuminate\Database\Eloquent\Model;
9-
use Illuminate\Support\Facades\Blade;
7+
use Illuminate\Support\Arr;
108
use Pboivin\FilamentPeek\Pages\Concerns\HasBuilderPreview;
119
use Pboivin\FilamentPeek\Pages\Concerns\HasPreviewModal;
12-
use Pboivin\FilamentPeek\Support\Html;
13-
use SolutionForest\InspireCms\InspireCmsConfig;
14-
use SolutionForest\InspireCms\Models\Contracts\Content;
10+
use SolutionForest\InspireCms\Factories\PreviewFactory;
1511

1612
trait ContentPreviewEditorTrait
1713
{
@@ -20,29 +16,24 @@ trait ContentPreviewEditorTrait
2016

2117
protected function getBuilderPreviewView(string $builderName): ?string
2218
{
23-
$template = filled($this->data['template_id'] ?? null) ? InspireCmsConfig::getTemplateModelClass()::find($this->data['template_id']) : null;
24-
$template ??= $this->getDocumentType()?->getDefaultTemplate();
25-
26-
$templateContent = $template?->getContent();
27-
28-
if (! $template || blank($templateContent)) {
29-
Notification::make()
30-
->title(__('inspirecms::notification.template_not_found.title'))
31-
->body(__('inspirecms::notification.template_not_found.body'))
32-
->danger()
33-
->seconds(60)
34-
->send();
35-
36-
throw new Halt;
37-
}
38-
39-
return $templateContent;
19+
return 'handle by previewFactory';
4020
}
4121

4222
public static function renderBuilderPreview(string $view, array $data): string
4323
{
44-
return Html::injectPreviewModalStyle(
45-
Blade::render($view, $data)
24+
$extraData = Arr::except($data, [
25+
'documentType',
26+
'template',
27+
'recordData',
28+
'propertyData',
29+
]);
30+
return PreviewFactory::create()->renderContentPreview(
31+
documentType: $data['documentType'] ?? null,
32+
template: $data['template'] ?? null,
33+
content: $data['recordData'] ?? [],
34+
propertyData: $data['propertyData'] ?? [],
35+
locale: $data['locale'] ?? null,
36+
data: $extraData,
4637
);
4738
}
4839

@@ -55,49 +46,30 @@ public static function getBuilderEditorSchema(string $builderName): \Filament\Fo
5546

5647
public function mutateInitialBuilderEditorData(string $builderName, array $editorData): array
5748
{
58-
$contentModel = $this->getModel();
59-
$editorData['contentModel'] = $contentModel;
60-
61-
$documentType = $this->getDocumentType();
62-
$editorData['documentType'] = $documentType instanceof Model ? $documentType->getKey() : $documentType;
49+
$editorData['recordData'] = Arr::except($this->data, [
50+
'propertyData',
51+
]);
52+
$editorData['propertyData'] = $this->data['propertyData'] ?? [];
6353

6454
if ($this instanceof CreateRecord) {
65-
$editorData['operation'] = 'create';
66-
$editorData['contentData'] = $this->data;
67-
$editorData['contentData']['children'] = [];
55+
$editorData['editorOperation'] = 'create';
56+
$editorData['recordData']['children'] = [];
57+
6858
} else {
69-
$editorData['operation'] = 'edit';
59+
$editorData['editorOperation'] = 'edit';
7060

7161
$content = $this->getRecord();
72-
$editorData['contentData'] = $this->data;
73-
$editorData['contentData']['children'] = $content->children()->pluck($content->getKeyName())->toArray();
74-
$editorData['contentKey'] = $content->getKey();
75-
62+
$editorData['recordData']['children'] = $content->children()->pluck($content->getKeyName())->toArray();
7663
}
7764

65+
$editorData['documentType'] = ($dt = $this->getDocumentType()) && $dt instanceof Model ? $dt->getKey() : ($dt ?? null); // primary key or model record
66+
$editorData['template'] = $this->data['template_id'] ?? null;
67+
7868
return $editorData;
7969
}
8070

8171
public static function mutateBuilderPreviewData(string $builderName, array $editorData, array $previewData): array
8272
{
83-
$contentModel = $editorData['contentModel'];
84-
85-
if (! in_array(Content::class, class_implements($contentModel))) {
86-
throw new \Exception('Model must implement ' . Content::class);
87-
}
88-
89-
$documentType = InspireCmsConfig::getDocumentTypeModelClass()::find($editorData['documentType']);
90-
91-
$contentDto = $contentModel::toPreviewDto(
92-
record: $editorData['contentData'],
93-
propertyData: $editorData['propertyData'] ?? [],
94-
locale: $editorData['activeLocale'],
95-
documentType: $documentType,
96-
);
97-
98-
$previewData['content'] = $contentDto;
99-
$previewData['locale'] = $editorData['activeLocale'] ?? app()->getLocale();
100-
101-
return $previewData;
73+
return array_merge($previewData, $editorData);
10274
}
10375
}

src/Base/Filament/Resources/Pages/BaseContentViewPage.php

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
namespace SolutionForest\InspireCms\Base\Filament\Resources\Pages;
44

55
use Filament\Actions;
6-
use Filament\Notifications\Notification;
76
use Filament\Resources\Pages\ViewRecord;
8-
use Filament\Support\Exceptions\Halt;
97
use Illuminate\Database\Eloquent\Model;
10-
use Illuminate\Support\Facades\Blade;
8+
use Illuminate\Support\Arr;
119
use Pboivin\FilamentPeek\Pages\Actions\PreviewAction;
1210
use Pboivin\FilamentPeek\Pages\Concerns\HasPreviewModal;
1311
use SolutionForest\InspireCms\Base\Filament\Concerns\ContentFormTrait;
1412
use SolutionForest\InspireCms\Base\Filament\Concerns\ContentPageTrait;
1513
use SolutionForest\InspireCms\Base\Filament\Contracts\ContentForm;
16-
use SolutionForest\InspireCms\Dtos\ContentDto;
14+
use SolutionForest\InspireCms\Factories\PreviewFactory;
1715
use SolutionForest\InspireCms\Filament\Actions\BackToParentContentAction;
1816
use SolutionForest\InspireCms\Filament\Actions\ContentHistoryAction;
1917
use SolutionForest\InspireCms\Filament\Actions\LockContentAction;
@@ -114,42 +112,38 @@ protected function configureAction(Actions\Action $action): void
114112
// region Preview
115113
protected function getPreviewModalView(): ?string
116114
{
117-
$record = $this->getRecord();
118-
$template = $record->getDefaultTemplate();
119-
$template ??= $this->getDocumentType()?->getDefaultTemplate();
120-
121-
$templateContent = $template?->getContent();
122-
123-
if (! $template || blank($templateContent)) {
124-
Notification::make()
125-
->title(__('inspirecms::notification.template_not_found.title'))
126-
->body(__('inspirecms::notification.template_not_found.body'))
127-
->danger()
128-
->seconds(60)
129-
->send();
130-
131-
throw new Halt;
132-
}
133-
134-
return $templateContent;
115+
return 'handle by previewFactory';
135116
}
136117

137118
public static function renderPreviewModalView(string $view, array $data): string
138119
{
139-
return Blade::render($view, $data);
120+
$extraData = Arr::except($data, [
121+
'propertyData',
122+
'content',
123+
'documentType',
124+
'template',
125+
'record',
126+
]);
127+
return PreviewFactory::create()->renderContentPreview(
128+
documentType: $data['documentType'] ?? null,
129+
template: $data['template'] ?? null,
130+
content: $data['content'] ?? null,
131+
propertyData: $data['propertyData'] ?? [],
132+
locale: $data['locale'] ?? null,
133+
data: $extraData,
134+
);
140135
}
141136

142137
protected function mutatePreviewModalData(array $data): array
143138
{
144-
$contentDto = $this->contentDto;
145-
146139
$locale = $this->getActiveFormsLocale();
147-
if ($contentDto instanceof ContentDto) {
148-
// Set the locale of the content dto to the active locale
149-
$contentDto->setLocale($locale);
150-
}
140+
$content = $this->getRecord();
151141

152-
$data['content'] = $contentDto;
142+
$data['propertyData'] = $content->getLatestPublishedPropertyData();
143+
$data['content'] = $content;
144+
$data['documentType'] = $content->documentType;
145+
$data['template'] = $content->getDefaultTemplate() ?? $content->documentType?->getDefaultTemplate();
146+
$data['contentDTO'] = $this->contentDto;
153147
$data['locale'] = $locale;
154148

155149
return $data;

0 commit comments

Comments
 (0)