Skip to content

Commit 1d68aa7

Browse files
committed
feat(laravel): update json view compiler to not render disabled blocks
1 parent 7933fb0 commit 1d68aa7

File tree

4 files changed

+125
-0
lines changed

4 files changed

+125
-0
lines changed

packages/laravel/src/Craftile.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class Craftile
1313

1414
protected $regionViewResolver = null;
1515

16+
protected $renderBlockChecker = null;
17+
1618
public function __construct(
1719
protected BlockSchemaRegistry $schemaRegistry,
1820
protected PropertyTransformerRegistry $transformerRegistry,
@@ -37,6 +39,14 @@ public function detectPreviewUsing(callable $detector): void
3739
$this->previewModeCache = null;
3840
}
3941

42+
/**
43+
* Register a custom block render checker.
44+
*/
45+
public function checkIfBlockCanRenderUsing(callable $checker): void
46+
{
47+
$this->renderBlockChecker = $checker;
48+
}
49+
4050
/**
4151
* Check if currently in preview mode.
4252
*/
@@ -88,6 +98,18 @@ public function endBlock(string $blockId): void
8898
$this->previewDataCollector->endBlock($blockId);
8999
}
90100

101+
/**
102+
* Check if a block should be rendered.
103+
*/
104+
public function shouldRenderBlock(BlockData $blockData): bool
105+
{
106+
if ($this->renderBlockChecker) {
107+
return call_user_func($this->renderBlockChecker, $blockData);
108+
}
109+
110+
return ! $blockData->disabled;
111+
}
112+
91113
/**
92114
* Start tracking content region (page-specific content).
93115
*/

packages/laravel/src/View/JsonViewCompiler.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ protected function compileBlock($blockData, $template, $path): string
284284
return <<<PHP
285285
<?php $blockDataVar = \\Craftile\\Laravel\\Facades\\BlockDatastore::getBlock("{$blockData['id']}"); ?>
286286
287+
<?php if (craftile()->shouldRenderBlock($blockDataVar)): ?>
288+
287289
<?php if (craftile()->inPreview()) {
288290
craftile()->startBlock("{$blockData['id']}", $blockDataVar);
289291
} ?>
@@ -295,6 +297,8 @@ protected function compileBlock($blockData, $template, $path): string
295297
craftile()->endBlock("{$blockData['id']}");
296298
} ?>
297299
300+
<?php endif; ?>
301+
298302
<?php unset($blockDataVar); ?>
299303
PHP;
300304
}

tests/laravel/Unit/CraftileTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,71 @@
5757

5858
expect($result)->toBe('themes.default.regions.sidebar');
5959
});
60+
61+
test('shouldRenderBlock returns true for enabled blocks by default', function () {
62+
$craftile = app(Craftile::class);
63+
$blockData = \Craftile\Laravel\BlockData::make([
64+
'id' => 'test-block',
65+
'type' => 'test',
66+
'disabled' => false,
67+
]);
68+
69+
expect($craftile->shouldRenderBlock($blockData))->toBeTrue();
70+
});
71+
72+
test('shouldRenderBlock returns false for disabled blocks by default', function () {
73+
$craftile = app(Craftile::class);
74+
$blockData = \Craftile\Laravel\BlockData::make([
75+
'id' => 'test-block',
76+
'type' => 'test',
77+
'disabled' => true,
78+
]);
79+
80+
expect($craftile->shouldRenderBlock($blockData))->toBeFalse();
81+
});
82+
83+
test('shouldRenderBlock can use custom checker', function () {
84+
$craftile = app(Craftile::class);
85+
86+
// Custom logic: only render blocks with 'public' in their ID
87+
$craftile->checkIfBlockCanRenderUsing(function ($blockData) {
88+
return str_contains($blockData->id, 'public');
89+
});
90+
91+
$publicBlock = \Craftile\Laravel\BlockData::make([
92+
'id' => 'public-block',
93+
'type' => 'test',
94+
]);
95+
96+
$privateBlock = \Craftile\Laravel\BlockData::make([
97+
'id' => 'private-block',
98+
'type' => 'test',
99+
]);
100+
101+
expect($craftile->shouldRenderBlock($publicBlock))->toBeTrue();
102+
expect($craftile->shouldRenderBlock($privateBlock))->toBeFalse();
103+
});
104+
105+
test('custom render checker overrides default disabled logic', function () {
106+
$craftile = app(Craftile::class);
107+
108+
// Custom logic that ignores disabled flag and only checks type
109+
$craftile->checkIfBlockCanRenderUsing(function ($blockData) {
110+
return $blockData->type === 'always-render';
111+
});
112+
113+
$disabledButAllowedType = \Craftile\Laravel\BlockData::make([
114+
'id' => 'test-block',
115+
'type' => 'always-render',
116+
'disabled' => true, // This should be ignored
117+
]);
118+
119+
$enabledButWrongType = \Craftile\Laravel\BlockData::make([
120+
'id' => 'test-block',
121+
'type' => 'never-render',
122+
'disabled' => false,
123+
]);
124+
125+
expect($craftile->shouldRenderBlock($disabledButAllowedType))->toBeTrue();
126+
expect($craftile->shouldRenderBlock($enabledButWrongType))->toBeFalse();
127+
});

tests/laravel/Unit/View/JsonViewCompilerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,34 @@
339339
expect($blocksToInvalidate)->toContain('grandparent'); // Grandparent
340340
expect($blocksToInvalidate)->toHaveCount(3);
341341
});
342+
343+
test('skips rendering disabled blocks', function () {
344+
$templateData = [
345+
'blocks' => [
346+
'enabled' => [
347+
'id' => 'enabled',
348+
'type' => 'test',
349+
'properties' => ['content' => 'This should render'],
350+
],
351+
'disabled' => [
352+
'id' => 'disabled',
353+
'type' => 'test',
354+
'properties' => ['content' => 'This should not render'],
355+
'disabled' => true,
356+
],
357+
],
358+
'regions' => [
359+
['name' => 'main', 'blocks' => ['enabled', 'disabled']],
360+
],
361+
];
362+
363+
$compiled = $this->compiler->compileTemplate($templateData);
364+
365+
// Should contain check for shouldRenderBlock
366+
expect($compiled)->toContain('craftile()->shouldRenderBlock');
367+
expect($compiled)->toContain('endif');
368+
369+
// Should contain both block IDs in the compiled output
370+
expect($compiled)->toContain('enabled');
371+
expect($compiled)->toContain('disabled');
372+
});

0 commit comments

Comments
 (0)