Skip to content

Add current form data as second argument to RecordFinder scope method - #1441

Open
mjauvin wants to merge 1 commit into
developfrom
record-finder-scope
Open

Add current form data as second argument to RecordFinder scope method#1441
mjauvin wants to merge 1 commit into
developfrom
record-finder-scope

Conversation

@mjauvin

@mjauvin mjauvin commented Jan 18, 2026

Copy link
Copy Markdown
Member

It can be useful if the record finder needs some other fields in the form to filter its values.

Related: wintercms/docs#256

E.g. fields definition:

country:
    type: dropdown

state:
    type: recordfinder
    list: $/author/plugin/models/state/columns.yaml
    scope: statesByCountry
class State extends model
{
    public function scopeStatesByCountry($query, $model, $formData)
    {
        if ($country_id = array_get($formData, 'country')) {
            $query->where('country_id', $country_id);
        }
    }
}

@mjauvin
mjauvin requested a review from LukeTowers January 18, 2026 17:24
@mjauvin mjauvin self-assigned this Jan 18, 2026
@mjauvin mjauvin added the enhancement PRs that implement a new feature or substantial change label Jan 18, 2026
@AIC-BV

AIC-BV commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

Works perfectly! Thanks!!!

public function scopeWhereProduct($query, $model, $formData)
{
    if ($productId = array_get($formData, 'product')) {
        return $query->where('product_id', $productId);
    }
    return $query->where('product_id', null); // returns an empty list in my case, so they will be forced to select a product first ;)
}

@AIC-BV

AIC-BV commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

@mjauvin can this be merged?
I had trouble because I ran composer update

@mjauvin

mjauvin commented Mar 10, 2026

Copy link
Copy Markdown
Member Author

@AIC-BV did you test this PR as well?

@AIC-BV

AIC-BV commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

@AIC-BV did you test this PR as well?

I copied the change to my local file and deployed it in production since Jan 18
Works perfectly

@mjauvin

mjauvin commented Mar 10, 2026

Copy link
Copy Markdown
Member Author

@LukeTowers any objection in merging this? There's a PR to the docs also in the description.

@mjauvin mjauvin added this to the 1.2.13 milestone Mar 13, 2026
@LukeTowers LukeTowers modified the milestones: 1.2.13, 1.3.0 Jun 10, 2026
@JonasPardon

Copy link
Copy Markdown

@LukeTowers Also running into this, could this be merged please? 🙏🏻

@LukeTowers

Copy link
Copy Markdown
Member

@mjauvin does this match any other scope method calls in the core? Seems somewhat arbitrary for your specific use case. @JonasPardon @AIC-BV can you provide examples of how you're using it?

@AIC-BV

AIC-BV commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

@LukeTowers concrete case from our webshop plugin (running patched in production since January):

An OrderProduct has a product recordfinder and a variant recordfinder. A variant only exists within a product, so the variant list has to be filtered by the product that was just picked — in the same form, which isn't saved yet. $this->model alone can't do it: on a create form product_id is still null, so you either list every variant in the catalogue or none.

product:
    type: recordfinder
    list: ~/plugins/aic/webshop/models/product/columns.yaml

variant:
    type: recordfinder
    list: ~/plugins/aic/webshop/models/variant/columns.yaml
    scope: whereProduct
public function scopeWhereProduct($query, $model, $formData = [])
{
    $productId = array_get($formData, 'product') ?: $model->product_id;

    return $query->where('product_id', $productId); // null => empty list, forces picking a product first
}

Same shape as Marc's country → state: any parent → child recordfinder pair hits this. And since PHP ignores extra arguments on userland methods, existing single-argument scopes keep working unchanged.

In short, picking a product fills in the variant picker
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement PRs that implement a new feature or substantial change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants