From abb7c502b9e5ca5d1873913222add44e2597bc43 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 29 May 2024 16:16:41 -0400 Subject: [PATCH 1/2] add test for using select param on nav tag --- tests/Tags/StructureTagTest.php | 73 +++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) diff --git a/tests/Tags/StructureTagTest.php b/tests/Tags/StructureTagTest.php index b37c39071d..4f9a059abe 100644 --- a/tests/Tags/StructureTagTest.php +++ b/tests/Tags/StructureTagTest.php @@ -80,6 +80,79 @@ public function it_renders_a_nav() ])); } + /** @test */ + public function it_renders_a_nav_with_selected_fields() + { + $this->createCollectionAndNav(); + + // The html uses tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion. + $template = <<<'EOT' + +EOT; + + $expected = <<<'EOT' + +EOT; + + $parsed = (string) Antlers::parse($template, [ + 'foo' => 'bar', // to test that cascade is inherited. + 'title' => 'outer title', // to test that cascade the page's data takes precedence over the cascading data. + ]); + + // This is really what we're interested in testing. The "Two" entry has a foo value + // of "notbar", but we're only selecting the title, so we shouldn't get the real value. + if (str_contains($parsed, 'Two notbar')) { + $this->fail('The "Two" entry\'s "foo" value was included.'); + } + + $this->assertXmlStringEqualsXmlString($expected, $parsed); + } + /** @test */ public function it_renders_a_nav_with_scope() { From 57121002cbf25696f7405eced9453709a7b4662b Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Wed, 29 May 2024 16:26:23 -0400 Subject: [PATCH 2/2] accept keys --- src/Data/BulkAugmentor.php | 16 +++++++++++++--- src/Tags/Structure.php | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Data/BulkAugmentor.php b/src/Data/BulkAugmentor.php index d4481f3057..cb33c7315f 100644 --- a/src/Data/BulkAugmentor.php +++ b/src/Data/BulkAugmentor.php @@ -10,6 +10,7 @@ class BulkAugmentor private $isTree = false; private $originalValues = []; private $augmentedValues = []; + private ?array $keys = null; private function getAugmentationReference($item) { @@ -25,9 +26,18 @@ public static function make($items) return (new static)->augment($items); } - public static function tree($tree) + public static function tree($tree, $keys = null) { - return (new static)->augmentTree($tree); + return (new static) + ->withKeys($keys) + ->augmentTree($tree); + } + + public function withKeys(?array $keys) + { + $this->keys = $keys; + + return $this; } /** @@ -54,7 +64,7 @@ private function augment($items) } $augmented = $item->augmented(); - $referenceKeys[$reference] = $augmented->keys(); + $referenceKeys[$reference] = $this->keys ?? $augmented->keys(); $referenceFields[$reference] = $augmented->blueprintFields(); } diff --git a/src/Tags/Structure.php b/src/Tags/Structure.php index bc7eb603f9..3bf57d1082 100644 --- a/src/Tags/Structure.php +++ b/src/Tags/Structure.php @@ -121,7 +121,7 @@ protected function isQueryingStatus() public function toArray($tree, $parent = null, $depth = 1) { - $pages = BulkAugmentor::tree($tree)->map(function ($item, $data, $index) use ($depth, $tree, $parent) { + $pages = BulkAugmentor::tree($tree, $this->params->explode('select'))->map(function ($item, $data, $index) use ($depth, $tree, $parent) { $page = $item['page']; $children = empty($item['children']) ? [] : $this->toArray($item['children'], $page, $depth + 1);