Skip to content

Commit eaa66b5

Browse files
authored
[5.x] Allow closure in cascade content hydration (#13580)
1 parent 3cf2cb4 commit eaa66b5

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/View/Cascade.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@ protected function hydrateContent()
158158
return $this;
159159
}
160160

161+
if ($this->content instanceof \Closure) {
162+
$this->content = call_user_func($this->content);
163+
}
164+
161165
$variables = $this->content instanceof Augmentable
162166
? $this->content->toDeferredAugmentedArray()
163167
: $this->content->toArray();

tests/View/CascadeTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,32 @@ public function it_hydrates_page_data()
410410
});
411411
}
412412

413+
#[Test]
414+
public function it_hydrates_page_data_by_closure()
415+
{
416+
$vars = ['foo' => 'bar', 'baz' => 'qux'];
417+
$page = EntryFactory::id('test')
418+
->collection('example')
419+
->data($vars)
420+
->make();
421+
$cascade = $this->cascade()->withContent(fn () => $page);
422+
423+
$this->assertEquals($page, call_user_func($cascade->content()));
424+
425+
tap($cascade->hydrate()->toArray(), function ($cascade) use ($page) {
426+
$this->assertArrayHasKey('page', $cascade);
427+
$this->assertEquals($page, $cascade['page']);
428+
429+
// The 'page' values should also be at the top level.
430+
// They'll be Value classes so Antlers can lazily augment them.
431+
// Blade can prefer {{ $globalhandle->field }} over just {{ $field }}
432+
$this->assertEquals('bar', $cascade['foo']);
433+
$this->assertEquals('qux', $cascade['baz']);
434+
$this->assertInstanceOf(Value::class, $cascade['foo']);
435+
$this->assertInstanceOf(Value::class, $cascade['baz']);
436+
});
437+
}
438+
413439
#[Test]
414440
public function it_hydrates_globals()
415441
{

0 commit comments

Comments
 (0)