Skip to content

[5.x] Improve recursive behavior when using route data in computed values #11855

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

Open
wants to merge 3 commits into
base: 5.x
Choose a base branch
from
Open
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
15 changes: 12 additions & 3 deletions src/Data/ContainsComputedData.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Statamic\Data;

use Statamic\Fields\Value;

trait ContainsComputedData
{
protected $withComputedData = true;
Expand All @@ -16,14 +18,21 @@ public function computedKeys()
}

public function computedData()
{
return $this->getComputedData(false);
}

public function getComputedData($wrapInValue)
{
if (! method_exists($this, 'getComputedCallbacks')) {
return collect();
}

return collect($this->getComputedCallbacks())->map(function ($callback, $field) {
return $this->getComputed($field);
});
return collect($this->getComputedCallbacks())
->map(fn ($callback, $field) => $wrapInValue ?
new Value(fn () => $this->getComputed($field)) :
$this->getComputed($field)
);
}

public function getComputed($key)
Expand Down
7 changes: 6 additions & 1 deletion src/Data/HasOrigin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public function keys()
}

public function values()
{
return $this->getValues(false);
}

public function getValues($wrapComputed)
{
$originFallbackValues = method_exists($this, 'getOriginFallbackValues') ? $this->getOriginFallbackValues() : collect();

$originValues = $this->hasOrigin() ? $this->origin()->values() : collect();

$computedData = method_exists($this, 'computedData') ? $this->computedData() : [];
$computedData = method_exists($this, 'getComputedData') ? $this->getComputedData($wrapComputed) : [];

return collect()
->merge($originFallbackValues)
Expand Down
7 changes: 6 additions & 1 deletion src/Entries/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,12 @@ public function route()

public function routeData()
{
$data = $this->values()->merge([
// This uses the `getValues(true)` method instead of values()
// This is so we can wrap computed fields in Value so we
// can delay their execution. If the computed value
// triggers the routeData() method, we will end
// up in an infinite loop that is not fun.
$data = $this->getValues(true)->merge([
'id' => $this->id(),
'slug' => $this->slug(),
'published' => $this->published(),
Expand Down
24 changes: 24 additions & 0 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2615,4 +2615,28 @@ public function it_clones_internal_collections()
$this->assertEquals('A', $entry->getSupplement('bar'));
$this->assertEquals('B', $clone->getSupplement('bar'));
}

#[Test]
public function using_route_data_in_computed_props_does_not_cause_infinite_loops()
{
$collection =
\Statamic\Facades\Collection::make('pages')
->routes('{slug}')
->structureContents(['root' => true]) // We need to be in a structure to create the infinite loop condition.
->save();

\Statamic\Facades\Collection::computed('pages', 'custom', function ($entry) {
return 'Custom: '.$entry->uri();
});

EntryFactory::id('entry-id')
->slug('entry-slug')
->collection('pages')
->create();

Blink::store('entry-uris')->flush();

$entry = Facades\Entry::find('entry-id');
$this->assertSame('Custom: /', $entry->custom);
}
}