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

Improve query params manipulation #2638

Merged

Conversation

piljac1
Copy link
Contributor

@piljac1 piljac1 commented Oct 12, 2020

This pull request adds the following:

  1. The current_full_url cascade contextual variable which behaves the same as current_url but includes query parameters
  2. An add_query_param modifier which adds the specified key/value pair to the URL as a valid query param
  3. A set_query_param modifier which adds the specified key/value pair to the URL (or updates the matching key) as a valid query param
  4. A remove_query_param modifier which removes the query param matching the specified key (if it exists) from the URL

Usage

{{ url add_query_param="category|dogs" }}
{{ url set_query_param="category|dogs" }}
{{ url remove_query_param="category" }}

<!-- or -->

{{ url add_query_param:category:dogs }}
{{ url set_query_param:category:dogs }}
{{ url remove_query_param:category }}

Todo list

  • Add a current_full_url cascade contextual variable which returns the complete URL
  • Add an add_query_param modifier which adds the specified key/value pair to the URL as a valid query param
  • Add a set_query_param which adds the specified key/value pair to the URL (or updates the matching key) as a valid query param
  • Add a remove_query_param which removes the query param matching the specified key (if it exists) from the URL
  • Make sure to preserve the anchor tag if it was specified

As for the implementation, there's a way of doing it with regex, but it's not much shorter, so contact me if you want a regex version (already saved a version aside for the setQueryParam modifier).

@piljac1 piljac1 marked this pull request as draft October 12, 2020 09:55
@piljac1 piljac1 marked this pull request as ready for review October 12, 2020 17:27
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.

This is great.

If you could apply the simple style changes listed here, that would be great.

https://github.styleci.io/analyses/ZlMv6b?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link

@piljac1
Copy link
Contributor Author

piljac1 commented Oct 12, 2020

This is great.

If you could apply the simple style changes listed here, that would be great.

https://github.styleci.io/analyses/ZlMv6b?utm_campaign=vcs-integration-link&utm_medium=referral&utm_source=github-build-link

Done !

@jasonvarga jasonvarga merged commit 29014f9 into statamic:master Oct 13, 2020
@jasonvarga
Copy link
Member

Awesome, thanks!

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