Skip to content

Commit 7cbf760

Browse files
committed
Fix template preview with livewire:
- ensure using filament-peek^4.0 - using internalUrl from filament-peek instead of using view
1 parent 270211d commit 7cbf760

File tree

8 files changed

+101
-89
lines changed

8 files changed

+101
-89
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"filament/spatie-laravel-translatable-plugin": "^3.2",
2828
"guava/filament-icon-picker": "^2.0",
2929
"khatabwedaa/blade-css-icons": "^1.5",
30-
"pboivin/filament-peek": "^2.0",
30+
"pboivin/filament-peek": "^4.0",
3131
"solution-forest/filament-field-group": "^1.0.14",
3232
"solution-forest/filament-tree": "^2.1.0",
3333
"solution-forest/inspirecms-support": "self.version",

config/inspirecms.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
'override_plugins' => [
6161
'field_group_models' => true, // Whether to override field group models
6262
'spatie_permission' => true, // Whether to override Spatie Permission package functionality
63+
'filament_peek' => true, // Whether to override Filament Peek package functionality
6364
],
6465
],
6566

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ protected function configureAction(Action $action): void
115115
}
116116
}
117117

118-
// region Preview
118+
//region Preview
119119
protected function getPreviewModalView(): ?string
120-
{
120+
{
121121
return 'handle by previewFactory';
122122
}
123123

@@ -155,9 +155,9 @@ protected function mutatePreviewModalData(array $data): array
155155

156156
return $data;
157157
}
158-
// endregion Preview
158+
//endregion Preview
159159

160-
// region Computed properties
160+
//region Computed properties
161161
#[Computed(persist: true, seconds: 7200)]
162162
public function contentDto()
163163
{
@@ -174,5 +174,5 @@ public function contentDto()
174174

175175
return null;
176176
}
177-
// endregion Computed properties
177+
//endregion Computed properties
178178
}

src/CmsPanelProvider.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function configureCmsPanel(Panel $panel)
7777

7878
$panel = $panel
7979
->bootUsing(function () {
80-
80+
8181
$skipSuperAdminCheck = AuthHelper::skipSuperAdminCheck();
8282
if ($skipSuperAdminCheck == 'before') {
8383
Gate::before(function ($user, $ability) {
@@ -202,7 +202,8 @@ protected function configurePlugins(Panel $panel): Panel
202202

203203
$plugins[] = $translatablePlugin;
204204

205-
return $panel->plugins($plugins);
205+
return $panel
206+
->plugins($plugins);
206207
}
207208

208209
protected function configureNavigation(Panel $panel): Panel

src/Content/DefaultPreviewProvider.php

Lines changed: 69 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use SolutionForest\InspireCms\Dtos\ContentDto;
1010
use SolutionForest\InspireCms\Dtos\PropertyTypeDto;
1111
use SolutionForest\InspireCms\Helpers\PropertyTypeHelper;
12+
use SolutionForest\InspireCms\Helpers\TemplateHelper;
1213
use SolutionForest\InspireCms\InspireCmsConfig;
1314
use SolutionForest\InspireCms\Models\Contracts\Content;
1415
use SolutionForest\InspireCms\Models\Contracts\DocumentType;
@@ -19,7 +20,51 @@ class DefaultPreviewProvider implements PreviewProviderInterface
1920
'isPeekPreviewModal' => true,
2021
];
2122

23+
protected static $previewType = 'internalUrl'; // internalUrl, view
24+
25+
public function getPeekPreviewType(): string
26+
{
27+
return static::$previewType;
28+
}
29+
30+
public function configureFilamentPeekAsInternalLink(): void
31+
{
32+
if ($this->getPeekPreviewType() === 'internalUrl') {
33+
config()->set('filament-peek.builderEditor.useInternalPreviewUrl', true);
34+
config()->set('filament-peek.internalPreviewUrl.enabled', true);
35+
}
36+
}
37+
2238
public function renderContentPreview($documentType, $content, $template, $locale = null, $propertyData = [], $data = [])
39+
{
40+
[$htmlContent, $viewData] = $this->prepareContentPreviewContentAndData(
41+
documentType: $documentType,
42+
content: $content,
43+
template: $template,
44+
locale: $locale,
45+
propertyData: $propertyData,
46+
data: $data
47+
);
48+
49+
return $this->renderBuilderPreview(
50+
Blade::render($htmlContent,$viewData, true)
51+
);
52+
}
53+
54+
public function renderTemplatePreview($templateContent, $documentType, $theme = null, $locale = null, $data = [])
55+
{
56+
if (empty($templateContent)) {
57+
return '';
58+
}
59+
60+
[$htmlContent, $viewData] = $this->prepareTemplatePreviewContentAndData($templateContent, $documentType, $theme, $locale, $data);
61+
62+
return $this->renderBuilderPreview(
63+
Blade::render($templateContent, $viewData)
64+
);
65+
}
66+
67+
protected function prepareContentPreviewContentAndData($documentType, $content, $template, $locale = null, $propertyData = [], $data = [])
2368
{
2469
$documentType = $this->findDocumentType($documentType);
2570
if (! $documentType) {
@@ -30,7 +75,7 @@ public function renderContentPreview($documentType, $content, $template, $locale
3075
->seconds(60)
3176
->send();
3277

33-
return $this->renderBuilderPreview('Document type not found');
78+
return ['Document type not found', []];
3479
}
3580

3681
$locale ??= $data['activeLocale'] ?? $data['locale'] ?? null;
@@ -53,8 +98,7 @@ public function renderContentPreview($documentType, $content, $template, $locale
5398
->danger()
5499
->seconds(60)
55100
->send();
56-
57-
return $this->renderBuilderPreview('Content not found');
101+
return ['Content not found', []];
58102
}
59103

60104
$templateContent = $this->findTemplateContent($template) ?? $this->findTemplateContent($documentType->getDefaultTemplate());
@@ -66,33 +110,23 @@ public function renderContentPreview($documentType, $content, $template, $locale
66110
->seconds(60)
67111
->send();
68112

69-
return $this->renderBuilderPreview('Template not found');
113+
return ['Template not found', []];
70114
}
71115

72116
if ($contentDTO instanceof ContentDto) {
73117
// Set the locale of the content dto to the active locale
74118
$contentDTO = $contentDTO->setLocale($locale);
75119
}
76120

77-
return $this->renderBuilderPreview(
78-
Blade::render(
79-
$templateContent,
80-
array_merge([
81-
'locale' => $locale,
82-
'content' => $contentDTO,
83-
... self::PREVIEW_DATA,
84-
], $data),
85-
true
86-
)
87-
);
121+
return [$templateContent, array_merge([
122+
'locale' => $locale,
123+
'content' => $contentDTO,
124+
... self::PREVIEW_DATA,
125+
], $data)];
88126
}
89127

90-
public function renderTemplatePreview($templateContent, $documentType, $theme = null, $locale = null, $data = [])
128+
protected function prepareTemplatePreviewContentAndData($htmlContent, $documentType, $theme = null, $locale = null, $data = [])
91129
{
92-
if (empty($templateContent)) {
93-
return '';
94-
}
95-
96130
$documentType = $this->findDocumentType($documentType);
97131
if (! $documentType) {
98132
Notification::make()
@@ -101,8 +135,7 @@ public function renderTemplatePreview($templateContent, $documentType, $theme =
101135
->danger()
102136
->seconds(60)
103137
->send();
104-
105-
return $this->renderBuilderPreview('Document type not found');
138+
return ['Document type not found', []];
106139
}
107140

108141
$contentDTO = $this->buildFakeContentDto($documentType, $locale);
@@ -116,29 +149,33 @@ public function renderTemplatePreview($templateContent, $documentType, $theme =
116149
if ($documentType->isDataType() && ! preg_match("/getComponentWithTheme\(\'(.*?)\'\)/", $templateContent)) {
117150

118151
// get the layout
119-
$layoutName = inspirecms_templates()->getComponentWithTheme('layout');
152+
$layoutName = inspirecms_templates()->getComponentWithTheme(TemplateHelper::getDefaultThemedLayoutComponentName());
120153

121154
if (view()->exists('components.' . $layoutName)) {
122155

123-
$newHtmlContent = Blade::render(
124-
"@extends('components.$layoutName')" . $templateContent,
125-
array_merge($viewData, ['slot' => '', 'layoutName' => $layoutName]),
126-
);
156+
$viewData = array_merge($viewData, ['slot' => '', 'layoutName' => $layoutName]);
127157

128-
return $this->renderBuilderPreview($newHtmlContent);
158+
return ["@extends('components.$layoutName')" . $htmlContent, $viewData];
129159
}
130160
}
131161

132-
return $this->renderBuilderPreview(
133-
Blade::render($templateContent, $viewData)
134-
);
162+
return [$htmlContent, $viewData];
135163
}
136164

137165
/**
138166
* @param int|Model|null $template
139167
* @return ?string
140168
*/
141169
protected function findTemplateContent($template)
170+
{
171+
return $this->findTemplate($template)?->getContent() ?? null;
172+
}
173+
174+
/**
175+
* @param int|Model|null $template
176+
* @return \Illuminate\Database\Eloquent\Model|null
177+
*/
178+
protected function findTemplate($template)
142179
{
143180
if (is_null($template)) {
144181
return null;
@@ -147,7 +184,7 @@ protected function findTemplateContent($template)
147184
$template = InspireCmsConfig::getTemplateModelClass()::find($template);
148185
}
149186

150-
return $template?->getContent() ?? null;
187+
return $template;
151188
}
152189

153190
/**

src/Content/PreviewProviderInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99

1010
interface PreviewProviderInterface
1111
{
12+
/**
13+
* Get the preview type for the peek plugin.
14+
*
15+
* @return string The preview type identifier ('view' or 'internalUrl').
16+
*/
17+
public function getPeekPreviewType(): string;
18+
19+
public function configureFilamentPeekAsInternalLink(): void;
20+
1221
/**
1322
* @param int|Model|DocumentType|null $documentType
1423
* @param string|int|Model|Content|array|null $content
@@ -20,7 +29,6 @@ interface PreviewProviderInterface
2029
public function renderContentPreview($documentType, $content, $template, $locale = null, $propertyData = [], $data = []);
2130

2231
/**
23-
* @param string|null $template
2432
* @param int|Model|DocumentType|null $documentType
2533
* @param string|null $theme
2634
* @param ?string $locale

src/Filament/Resources/DocumentTypeResource/RelationManagers/TemplatesRelationManager.php

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
class TemplatesRelationManager extends RelationManager
2525
{
2626
use CanAuthorizeRelationManager;
27-
use HasBuilderPreview {
28-
openPreviewModalForBuidler as protected traitOpenPreviewModalForBuidler;
29-
}
27+
use HasBuilderPreview;
3028
use HasPreviewModal;
3129

3230
protected static string $relationship = 'templates';
@@ -154,6 +152,8 @@ public static function getTitle(Model $ownerRecord, string $pageClass): string
154152
return __('inspirecms::resources/document-type.templates.label');
155153
}
156154

155+
//region Actions
156+
157157
protected function configureCreateAction(CreateAction $action): void
158158
{
159159
parent::configureCreateAction($action);
@@ -267,50 +267,12 @@ protected function configureViewOrEditAction(Tables\Actions\Action $action)
267267
});
268268
}
269269
}
270+
//endregion Actions
270271

271-
// region Preview
272+
//region Preview
272273
protected function getBuilderPreviewView(string $builderName): ?string
273274
{
274-
$templateRecord = $this->cachedMountedTableActionRecord;
275-
if (! $templateRecord || ! ($templateRecord instanceof Template)) {
276-
return null;
277-
}
278-
279-
return $templateRecord->slug; // wouldn't use this, but it's required
280-
}
281-
282-
public function openPreviewModalForBuidler(string $builderName): void
283-
{
284-
$this->checkCustomListener();
285-
286-
$editorData = $this->mutateInitialBuilderEditorData(
287-
$builderName,
288-
$this->prepareBuilderEditorData($builderName)
289-
);
290-
291-
if (! isset($editorData['html_content']) || blank($editorData['html_content'])) {
292-
293-
Notification::make()
294-
->title(__('inspirecms::notification.template_not_found.title'))
295-
->body(__('inspirecms::notification.template_not_found.body'))
296-
->danger()
297-
->seconds(60)
298-
->send();
299-
300-
// Avoid opening the modal if the template is not found
301-
return;
302-
}
303-
304-
$this->dispatch(
305-
'openBuilderEditor',
306-
previewView: $this->getBuilderPreviewView($builderName),
307-
previewUrl: $this->getBuilderPreviewUrl($builderName),
308-
modalTitle: $this->getPreviewModalTitle(),
309-
editorTitle: $this->getBuilderEditorTitle(),
310-
editorData: $editorData,
311-
builderName: $builderName,
312-
pageClass: static::class,
313-
);
275+
return 'handle by previewFactory';
314276
}
315277

316278
public function mutateInitialBuilderEditorData(string $builderName, array $editorData): array
@@ -386,9 +348,9 @@ protected function getBuilderEditorTitle(): string
386348
{
387349
return __('inspirecms::resources/template.editor.title');
388350
}
389-
// endregion Preview
351+
//endregion Preview
390352

391-
// region Helpers
353+
//region Helpers
392354
protected function refreshPageAlerts(): void
393355
{
394356
$this->dispatch('refreshAlerts');
@@ -398,5 +360,5 @@ protected function assignDefaultTemplateIfNotSet($template): void
398360
{
399361
inspirecms_templates()->assignDefaultTemplateIfNotSet($this->getOwnerRecord(), $template);
400362
}
401-
// endregion Helpers
363+
//endregion Helpers
402364
}

src/InspireCmsServiceProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,10 @@ protected function customPlugins(): void
317317
if (InspireCmsConfig::get('system.override_plugins.spatie_permission', false)) {
318318

319319
config()->set('permission.enable_wildcard_permission', true);
320-
320+
}
321+
322+
if (InspireCmsConfig::get('system.override_plugins.filament_peek', true)) {
323+
PreviewFactory::create()->configureFilamentPeekAsInternalLink();
321324
}
322325
}
323326

0 commit comments

Comments
 (0)