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

Automatically append namespace to "getQueryParam" if used inside "namespace" tag #5936

Closed
piotrpog opened this issue Apr 16, 2020 · 5 comments
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development

Comments

@piotrpog
Copy link

piotrpog commented Apr 16, 2020

Today I tested {% namespace %} tag. I create simple form with input which would be filled with content of URL param corresponding to inputs name:

{% namespace 'xxx' %}
<form action="">
<label for="someId"></label>
<input type="text" id="someId" name="someName" value="{{craft.app.request.getQueryParam('someName')}}">
</form>
{% endnamespace  %}

Unfortunetly it did not worked. I needed to append namespace manually to getQueryParam:

{% set someNamespace = 'xxx' %}
{% namespace someNamespace %}
<form action="">
<label for="someId"></label>
<input type="text" id="someId" name="someName" value="{{craft.app.request.getQueryParam(someNamespace~'.someName')}}">
</form>
{% endnamespace  %}

I have proposal that would allow avoiding that - adding third param to getQueryParam that would automatically append namespace to getQueryParam function if it was used within namespace tag.

@piotrpog piotrpog changed the title Automatically append namespace to "getQueryParam" parame if used inside "namespace" tag Automatically append namespace to "getQueryParam" if used inside "namespace" tag Apr 16, 2020
@brandonkelly
Copy link
Member

This is what the namespaceInputName filter is for:

{{ craft.app.request.getQueryParam('someName'|namespaceInputName) }}

(As long as you put that code within your {% namespace %} tag pair, it will default to using the active namespace defined by that tag.)

@piotrpog
Copy link
Author

@brandonkelly
nameSpaceInputName returns format namespace[name]. getQueryParam requires format namespace.name.

That's why namespaceInputName cannot be used in this situation.

@brandonkelly
Copy link
Member

You’re right, sorry. Will reopen for now, but I think the solution here is maybe to make getQueryParam() support the square bracket syntax.

@brandonkelly brandonkelly reopened this Apr 17, 2020
@brandonkelly brandonkelly added enhancement improvements to existing features site development 👩‍💻 features related to website/API development labels Apr 17, 2020
@piotrpog
Copy link
Author

@brandonkelly
Wouldn't it be project breaking change?
Maybe it would be simpler to add a new filter like namespaceInputName that uses dot notation.

@brandonkelly
Copy link
Member

Actually this isn’t going to be possible at all, because Craft can’t always reliably know what a parameter name is going to end up as, solely based on its form input name.

For example, let’s say the input name is fields[myEntriesField][]. The actual value could end at $_GET['fields']['myEntriesField'][0], or $_GET['fields']['myEntriesField'][1], or any other index. (And that’s assuming that some other input further down in the form doesn’t completely override fields or fields[myEntriesField], which we can’t be sure of either.)

That makes me think this isn’t a good idea to address directly in Craft, but you could hack around it using a combination of namespaceInputName, replace, and trim:

{% macro namespaceParam(param) %}
    {{- param|namespaceInputName|replace('/[\\[\\]]+/', '.')|trim('.') -}}
{% endmacro %}

{{ craft.app.request.getQueryParam(_self.namespaceParam('someParam')) }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development
Projects
None yet
Development

No branches or pull requests

2 participants