statamic-rad-pack / runway

Eloquently manage your database models in Statamic.
https://statamic.com/addons/rad-pack/runway
MIT License
113 stars 49 forks source link

Query scope context on entries field #330

Closed panda4man closed 1 year ago

panda4man commented 1 year ago

Description

I'm trying to run a query scope on an entries field type where that scope uses contextual data to do the filtering. That contextual data needs to be such that I'm able to run a query based on this data to fetch further data to subsequently use in the scope.

More specifically, I have a Runway collection called Products. This collection has two fields in particular that are relevant to this issue:

On any given product details page in the CP, I want to be able to filter the available types via a query scope, using the selected value of Attribute Group to do this filtering.

I have a query scope called ProductAttributeTypeFilter which extends the base Statamic Statamic\Query\Scopes\Scope class.

The apply($query, $values) method receives an array of request-contextual data which I was hoping would contain information relevant to the base Runway parent product the page is on. It does not. Here's what that payload looks like:

array (
  'config' => 'eyJtb2RlIjoiZGVmYXVsdCIsImNyZWF0ZSI6ZmFsc2UsInRheG9ub21pZXMiOlsicHJvZHVjdF90eXBlIl0sInR5cGUiOiJ0ZXJtcyIsImRpc3BsYXkiOiJUeXBlcyIsImljb24iOiJ0YXhvbm9teSIsImxpc3RhYmxlIjoiaGlkZGVuIiwiaW5zdHJ1Y3Rpb25zX3Bvc2l0aW9uIjoiYWJvdmUiLCJ2aXNpYmlsaXR5IjoidmlzaWJsZSIsImhpZGVfZGlzcGxheSI6ZmFsc2UsIndpZHRoIjo1MCwicXVlcnlfc2NvcGVzIjpbInByb2R1Y3RfYXR0cmlidXRlX3R5cGVfZmlsdGVyIl0sIm1heF9pdGVtcyI6MCwiY29tcG9uZW50IjoicmVsYXRpb25zaGlwIiwiaGFuZGxlIjoidHlwZXMiLCJwcmVmaXgiOm51bGwsImluc3RydWN0aW9ucyI6bnVsbCwicmVxdWlyZWQiOmZhbHNlLCJyZWFkX29ubHkiOmZhbHNlLCJhbHdheXNfc2F2ZSI6ZmFsc2V9',
  'taxonomies' => 
  array (
    0 => 'product_type',
  ),
  'sort' => 'title',
  'order' => 'asc',
  'page' => '1',
  'filters' => 
  \Illuminate\Support\Collection::__set_state(array(
     'items' => 
    array (
    ),
     'escapeWhenCastingToString' => false,
  )),
  'columns' => 'title',
  'search' => NULL,
) 

In the context of this issue, this $values array comes from the following method:

Statamic\Fieldtypes\Entries::getIndexQuery() -> applyIndexQueryScopes($query, $request->all())

Based on what I can see, $request->all() is what is being passed down to the query scope as its second parameter $values.

I believe this endpoint is called by a Vue component, but at this point I'm wondering if I am going against the grain of the expected functionality of CP query scopes & filters.

Steps to reproduce

  1. Create a Runway resource
  2. Create a query scope for the target resource field
  3. Add an Entries field to that resource and assign the query scope to that field
  4. Try to filter the query in the query scope using data in the second param, $values

Environment

Environment Application Name: Statamic Peak Laravel Version: 10.18.0 PHP Version: 8.2.7 Composer Version: 2.5.8 Environment: local Debug Mode: ENABLED URL: alpine-power-statamic-2023.test Maintenance Mode: OFF

Cache Config: NOT CACHED Events: NOT CACHED Routes: NOT CACHED Views: CACHED

Drivers Broadcasting: log Cache: statamic Database: mysql Logs: daily Mail: smtp Queue: redis Session: file

Statamic Data Importer Version: 0.0.1

Statamic Kentico Migrator Version: 0.0.1

Statamic Addons: 6 Antlers: runtime Stache Watcher: Enabled Static Caching: Disabled Version: 4.21.0 PRO

Statamic Addons doublethreedigital/runway: 5.2.0 fahlgrendigital/tools-for-statamic: 0.7 studio1902/statamic-peak-browser-appearance: 2.0 studio1902/statamic-peak-commands: 2.3.2 studio1902/statamic-peak-seo: 2.6.2 studio1902/statamic-peak-tools: 3.2

duncanmcclean commented 1 year ago

I've taken a look at how this works when using the Entries fieldtype on an entry. This is what $values looks like:

image

There's no context about the value of the other fields.

Since this isn't specifically a Runway issue (context not being passed to the query scope), but rather something that's not there in Statamic core, it might be worth opening an issue there instead.

panda4man commented 1 year ago

That's what I figured. Thanks, sir!