Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Bring back select param on nav tag #10226

Merged
merged 3 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Data/BulkAugmentor.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class BulkAugmentor
private $isTree = false;
private $originalValues = [];
private $augmentedValues = [];
private ?array $keys = null;

private function getAugmentationReference($item)
{
Expand All @@ -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;
}

/**
Expand All @@ -51,7 +61,7 @@ private function augment($items)
}

$augmented = $item->augmented();
$referenceKeys[$reference] = $augmented->keys();
$referenceKeys[$reference] = $this->keys ?? $augmented->keys();
$referenceFields[$reference] = $augmented->blueprintFields();
}

Expand Down
2 changes: 1 addition & 1 deletion src/Tags/Structure.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
67 changes: 65 additions & 2 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,72 @@ public function it_renders_a_nav_with_selected_fields()
{
$this->createCollectionAndNav();

$template = '{{ nav:test select="title" }}{{ *recursive children* }}{{ /nav:test }}';
// The html uses <i> tags (could be any tag, but i is short) to prevent whitespace comparison issues in the assertion.
$template = <<<'EOT'
<ul>
{{ nav:test select="title" }}
<li>
<i>{{ title }} {{ foo }}</i>
{{ if children }}
<ul>
{{ *recursive children* }}
</ul>
{{ /if }}
</li>
{{ /nav:test }}
</ul>
EOT;

$expected = <<<'EOT'
<ul>
<li>
<i>One bar</i>
<ul>
<li>
<i>One One bar</i>
</li>
<li>
<i>URL and title bar</i>
</li>
</ul>
</li>
<li>
<i>Two bar</i>
</li>
<li>
<i>Three bar</i>
<ul>
<li>
<i>Three One bar</i>
</li>
<li>
<i>Three Two bar</i>
</li>
</ul>
</li>
<li>
<i>Title only bar</i>
<ul>
<li>
<i>URL only bar</i>
</li>
</ul>
</li>
</ul>
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->assertNotNull(Antlers::parse($template));
$this->assertXmlStringEqualsXmlString($expected, $parsed);
}

#[Test]
Expand Down
Loading