Skip to content

Commit 7b47cce

Browse files
authored
[5.x] Fix page collection and mounted collection (#13250)
1 parent 08d9a54 commit 7b47cce

3 files changed

Lines changed: 37 additions & 2 deletions

File tree

src/Structures/Page.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,13 @@ public function collection()
476476
return Collection::findByMount($this);
477477
}
478478

479+
public function mountedCollection()
480+
{
481+
return ($entry = $this->entry())
482+
? Collection::findByMount($entry)
483+
: null;
484+
}
485+
479486
public function getProtectionScheme()
480487
{
481488
return optional($this->entry())->getProtectionScheme();

src/Structures/TreeBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function transformTreeForController($tree)
8989
{
9090
return collect($tree)->map(function ($item) {
9191
$page = $item['page'];
92-
$collection = $page->collection();
92+
$collection = $page->mountedCollection();
9393
$referenceExists = $page->referenceExists();
9494

9595
return [

tests/Data/Structures/PageTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPUnit\Framework\Attributes\Test;
1111
use Statamic\Contracts\Structures\Nav;
1212
use Statamic\Entries\Entry;
13+
use Statamic\Facades\Collection as Collections;
1314
use Statamic\Facades\Entry as EntryAPI;
1415
use Statamic\Structures\CollectionStructure;
1516
use Statamic\Structures\Page;
@@ -528,10 +529,37 @@ public function it_is_arrayable()
528529
->each(fn ($value, $key) => $this->assertEquals($value, $page->{$key}))
529530
->each(fn ($value, $key) => $this->assertEquals($value, $page[$key]));
530531

531-
$this->assertEquals($page->collection()->toArray(), $arr['collection']);
532+
$this->assertEquals($page->collection->toArray(), $arr['collection']);
532533
$this->assertEquals($page->blueprint->toArray(), $arr['blueprint']);
533534
}
534535

536+
#[Test]
537+
public function it_gets_collection_and_mounted_collection()
538+
{
539+
Collections::make('pages')->save();
540+
Collections::make('blog')->mount('blog-page')->save();
541+
Collections::make('events')->save();
542+
543+
$blogPageEntry = EntryFactory::id('blog-page')->collection('pages')->create();
544+
$blogPostEntry = EntryFactory::id('blog-post-one')->collection('blog')->create();
545+
$eventEntry = EntryFactory::id('event-one')->collection('events')->create();
546+
547+
$tree = $this->mock(Tree::class);
548+
$tree->shouldReceive('entry')->with('blog-page')->andReturn($blogPageEntry);
549+
$tree->shouldReceive('structure')->andReturnNull(); // just make the blueprint method quiet for now.
550+
551+
$page = new Page;
552+
$page->setTree($tree);
553+
$page->setEntry($blogPageEntry);
554+
$page->setId($blogPageEntry->id()); // In reality the tree would set this.
555+
$this->assertEquals('blog', $page->mountedCollection()->handle());
556+
$this->assertEquals('pages', $page->collection->handle());
557+
558+
// This should be "pages" but cannot be fixed without being a breaking change.
559+
// This will change in v6.
560+
$this->assertEquals('blog', $page->collection()->handle());
561+
}
562+
535563
protected function newTree()
536564
{
537565
return new class extends Tree

0 commit comments

Comments
 (0)