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] Add to_qs modifier #10196

Merged
merged 2 commits into from
May 28, 2024
Merged

Conversation

godismyjudge95
Copy link
Contributor

@godismyjudge95 godismyjudge95 commented May 24, 2024

This PR adds a simple to_qs modifier that passes the incoming array or array-like value to Laravel's Arr::query() helper method.

Example Data:

$params = [
    'mode' => 'plaid',
    'area' => [51, 52],
    'hat' => null,
    'transportation' => [
        'bike' => true,
        'delorian' => false,
    ],
];

Example Template:

<a href="/search?{{ params | to_qs }}">Search Now</a>

Example Output:

<a href="/search?mode=plaid&area%5B0%5D=51&area%5B1%5D=52&transportation%5Bbike%5D=1&transportation%5Bdelorian%5D=0">Search Now</a>

(I don't like the encoded characters any more than you do - but that is the output)


Edit: Changed the modifier to to_qs per the discussion below.

Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to keep nitpicking your PRs. They're good!

But the first thing I thought of when seeing to_query was that it was being converted to a query builder somehow.

Can we call it to_query_string or query_string or even just qs?

I don't mind qs as I feel like it's pretty a common abbreviation. There's even a popular npm package. https://www.npmjs.com/package/qs

@godismyjudge95
Copy link
Contributor Author

Sorry to keep nitpicking your PRs. They're good!

But the first thing I thought of when seeing to_query was that it was being converted to a query builder somehow.

Can we call it to_query_string or query_string or even just qs?

I don't mind qs as I feel like it's pretty a common abbreviation. There's even a popular npm package. npmjs.com/package/qs

Your nitpicks are always welcome 😄 I was struggling myself to think of a good name that made sense as I think a lot of people may have used "query" as a variable name.

What would you say to to_qs - I like the to_ pattern Statamic has going to denote a final transform (ie. to_json)

@jasonvarga
Copy link
Member

to_qs works too. 👍

@godismyjudge95 godismyjudge95 changed the title [5.x] Add to_query modifier [5.x] Add to_qs modifier May 24, 2024
@jasonvarga jasonvarga merged commit 6322d7b into statamic:5.x May 28, 2024
18 checks passed
@godismyjudge95 godismyjudge95 deleted the add-to-query-modifier branch May 28, 2024 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants