putyourlightson / craft-sprig

A reactive Twig component framework for Craft CMS.
https://putyourlightson.com/plugins/sprig
MIT License
129 stars 9 forks source link

Url parameters issue with pushUrl(). #361

Closed Shanyb closed 7 months ago

Shanyb commented 7 months ago

Support Request

I created a Sprig component to search on entries. I have many parameters of different types. String (search), checkbox, numbers.

The system sends the parameters in the URL using the pushUrl() function. However, if I retrieve this URL and insert it in a new tab, some URL parameters are not retrieved and used as search parameters. Sprig does not see URL parameters.

Should Sprig pick up parameters without other line of code? Do we need to do anything else to retrieve the URL parameters? The parameters seeming to cause problems are checkboxes (value="color[]").

The component works very well for search, it only has the URL parameter principle which is missing.

Plugin Version

No response

bencroker commented 7 months ago

Sprig should pick up URL parameters. Can you show me an example of a URL and some Twig code that demonstrates the issue, so I can test it locally?

Shanyb commented 7 months ago

Yes, sure.

URL: https://dev.lachance.qc.ca/proprietes-a-vendre?recherche=&prixmin=0&prixmax=1700000&mode=liste&type%5B0%5D=37&type%5B1%5D=7&secteur%5B0%5D=39335&secteur%5B1%5D=4736&secteur%5B2%5D=39329&plans=&ordre=price%20ASC&page=1

(the site have a password, still in dev)

For the code, it's related to a lot of thing. What part of the code would be useful?

EX:

{################################################ Craft Fields available ################################################}

{% set allPlans = craft.entries().section('blueprintEntries').relatedTo(entryWithField).title('not client').orderBy('title ASC').all() %} {% set sectorsAvailable = craft.categories().group('catZone').relatedTo(entryWithField).orderBy('title ASC').all() %}

{% set fielsHouseType = craft.app.fields.getFieldByHandle('houseType').options %} {% set houseTypeAvailable = [] %} {% set houseTypeAvailableID = [] %} {% for entry in entryWithField %} {% set houseTypeEl = entry.houseType %} {% if houseTypeEl|length %} {% for option in houseTypeEl %} {% if option.value not in houseTypeAvailableID %} {% set houseTypeAvailableID = houseTypeAvailableID|merge([option.value]) %} {% set houseTypeAvailable = houseTypeAvailable|merge([option]) %} {% endif %} {% endfor %} {% endif %} {% endfor %}

{% set houseTypeAvailable = houseTypeAvailable|multisort(e => e.label) %}

{################################################ Fields declaration ################################################} {% set search = search ?? '' %} {% set page = page ?? 1 %} {% set searchZone = searchZone ?? [] %} {% set priceRangeMin = priceRangeMin ?? 0 %} {% set priceRangeMax = priceRangeMax ?? defautlPrice %} {% set roomQt = roomQt ?? [] %} {% set stairQt = stairQt ?? [] %} {% set bathroomQt = bathroomQt ?? [] %} {% set houseReady = houseReady ?? [] %} {% set garage = garage ?? [] %} {% set mode = mode ?? 'liste' %}

{################################################ QUERY ################################################} {% set entryQuery = craft.entries({ search: search, section: 'forSaleHouseEntry', orderBy: orderBy, limit: 24, }) %}

{% if priceRangeMin is defined and priceRangeMax is defined %} {% if priceRangeMin == 0 %} {% do entryQuery.price(['and', ">= 1", "<= #{priceRangeMax}"]) %} {% else %} {% do entryQuery.price(['and', ">= #{priceRangeMin}", "<= #{priceRangeMax}"]) %} {% endif %} {% endif %}

{% if searchZone is not empty %} {% do entryQuery.relatedTo(searchZone) %} {% endif %}

{% if roomQt is not empty %} {% do entryQuery.roomQt(roomQt) %} {% endif %}

{% if stairQt is not empty %} {% do entryQuery.floorQt(stairQt) %} {% endif %}

{% if houseType is not empty %} {% do entryQuery.houseType(houseType) %} {% endif %}

{% if bathroomQt is not empty %} {% do entryQuery.bathroomQt(bathroomQt) %} {% endif %}

{% if plans != ""%} {% do entryQuery.relatedTo(plans) %} {% endif %}

{% if houseReady is not empty %} {% do entryQuery.forSaleHouseReadyStatus('houseReady').forSalePropertyStatus('not 0') %} {% endif %}

{% if garage is not empty %} {% do entryQuery.garageAvalaible(true) %} {% endif %}

{% do entryQuery.with(['sectorsCat', 'forSaleImg', 'forSaleImgCraft', 'housePlan']) %} {% set pageInfo = sprig.paginate(entryQuery, page) %} {% set entries = pageInfo.pageResults %}

{################################################ Some part of TWIG not all ################################################}

{{priceRangeMin|number_format(0, ' ', ' ')}}$

{{priceRangeMax|number_format(0, ' ', ' ')}}$

{% if entries|length %} {% for entry in entries %}

{{ entry.title }}
 {% endfor %}

{% endif %}

bencroker commented 7 months ago

Sorry, that is far too much code. If you can provide the smallest possible reproducible code, then I’ll gladly take a look and try to help you.

Shanyb commented 7 months ago

Sorry for that. While preparing the elements to send to you, I found the error which prevented Sprig from recovering the parameters. So we can consider this ticket resolved.

Thank you very much for the offer of help, much appreciated. I love the Sprig & Blitz expansions, amazing extention team.

bencroker commented 7 months ago

Glad to hear it!