Skip to content
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
7 changes: 6 additions & 1 deletion src/Fieldtypes/Relationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,12 @@ public function toQueryableValue($value)

protected function applyIndexQueryScopes($query, $params)
{
collect(Arr::wrap($this->config('query_scopes')))
$handles = Arr::wrap($this->config('query_scopes'));

// Pass the active handles along (like the asset browser) so an aliased scope knows which is in effect.
$params = array_merge($params, ['queryScopes' => $handles]);

collect($handles)
->map(fn ($handle) => Scope::find($handle))
->filter()
->each(fn ($scope) => $scope->apply($query, $params));
Expand Down
42 changes: 42 additions & 0 deletions tests/Feature/Fieldtypes/RelationshipFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public function setUp(): void
$this->collection = Collection::make('test')->save();

app('statamic.scopes')[StartsWithC::handle()] = StartsWithC::class;

// Register one scope class under an alias that differs from its handle, so the
// test can prove the configured alias is what reaches the scope.
app('statamic.scopes')['fruit_filter'] = AliasedScope::class;
}

#[Test]
Expand Down Expand Up @@ -64,6 +68,34 @@ public function it_filters_entries_by_query_scopes()
$this->assertNotContains('Banana', $titles);
}

#[Test]
public function it_passes_the_active_scope_handles_to_aliased_scopes()
{
Entry::make()->collection('test')->slug('apple')->data(['title' => 'Apple'])->save();
Entry::make()->collection('test')->slug('carrot')->data(['title' => 'Carrot'])->save();
Entry::make()->collection('test')->slug('cherry')->data(['title' => 'Cherry'])->save();
Entry::make()->collection('test')->slug('banana')->data(['title' => 'Banana'])->save();

$this->setTestRoles(['test' => ['access cp', 'view test entries']]);
$user = User::make()->assignRole('test')->save();

$config = base64_encode(json_encode([
'type' => 'entries',
'collections' => ['test'],
'query_scopes' => ['fruit_filter'],
]));

$response = $this
->actingAs($user)
->get("/cp/fieldtypes/relationship?config={$config}")
->assertOk();

$titles = collect($response->json('data'))->pluck('title')->all();

// The scope only filters when its alias is present in the queryScopes param.
$this->assertEqualsCanonicalizing(['Carrot', 'Cherry'], $titles);
}

#[Test]
public function it_limits_access_to_entries_from_collections_the_user_can_view()
{
Expand Down Expand Up @@ -716,3 +748,13 @@ public function apply($query, $params)
$query->where('title', 'like', 'C%');
}
}

class AliasedScope extends Scope
{
public function apply($query, $params)
{
if (in_array('fruit_filter', $params['queryScopes'] ?? [])) {
$query->where('title', 'like', 'C%');
}
}
}
Loading