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] Restore nav/structure select behavior #10240

Closed
wants to merge 2 commits into from
Closed
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
32 changes: 26 additions & 6 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 $keyResolver;

private function getAugmentationReference($item)
{
Expand All @@ -20,14 +21,21 @@ private function getAugmentationReference($item)
return 'Ref::'.get_class($item).spl_object_hash($item);
}

public static function make($items)
public static function make($items, $keyResolver = null)
{
return (new static)->augment($items);
return (new static)->resolveKeysWith($keyResolver)->augment($items);
}

public static function tree($tree)
public static function tree($tree, $keyResolver = null)
{
return (new static)->augmentTree($tree);
return (new static)->resolveKeysWith($keyResolver)->augmentTree($tree);
}

public function resolveKeysWith($callback)
{
$this->keyResolver = $callback;

return $this;
}

/**
Expand All @@ -49,12 +57,24 @@ private function augment($items)
$this->originalValues[$i] = $item;
}

if (array_key_exists($reference, $referenceKeys)) {
if (array_key_exists($reference, $referenceKeys) && $this->keyResolver === null) {
continue;
}

$augmented = $item->augmented();
$referenceKeys[$reference] = $augmented->keys();

if ($this->keyResolver) {
$keys = call_user_func($this->keyResolver, $augmented);

if ($keys === null) {
$keys = $augmented->keys();
}

$referenceKeys[$reference] = $keys;
} else {
$referenceKeys[$reference] = $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, fn ($item) => $this->getQuerySelectKeys($item))->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
17 changes: 17 additions & 0 deletions tests/Tags/StructureTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,23 @@ public function it_sets_is_current_and_is_parent_for_a_collection()
$this->assertEquals('[1=parent][1-1=parent][1-1-1=parent][1-1-1-1=current][2]', $result);
}

/** @test */
public function it_limits_fields_using_select()
{
$this->createCollectionAndNav();

$template = '{{ nav:test select="title" }}<title:{{ title }}><nav_title:{{ nav_title }}>{{ *recursive children* }}{{ /nav:test }}';

$result = (string) Antlers::parse($template);

// The nav_title should always be empty since we did not select it.
$expected = <<<'EXP'
<title:One><nav_title:><title:One One><nav_title:><title:URL and title><nav_title:><title:Two><nav_title:><title:Three><nav_title:><title:Three One><nav_title:><title:Three Two><nav_title:><title:Title only><nav_title:><title:URL only><nav_title:>
EXP;

$this->assertSame($expected, $result);
}

private function makeNav($tree)
{
$nav = Nav::make('test');
Expand Down
Loading