Last active
August 14, 2018 14:38
-
-
Save jackabox/d7bc229e7c82c48176f161855d308976 to your computer and use it in GitHub Desktop.
Querying based on request params in Craft 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# Set up the request params and queries. #} | |
{% set req = craft.request %} | |
{% set params = ['date', 'category'] %} | |
{% set query = {} %} | |
{% set search = [] %} | |
{% for param in params %} | |
{% set key = param | camel %} | |
{% set value = req.getParam(param) %} | |
{% set query = query | merge({ (key):(value) }) %} | |
{% endfor %} | |
{% if search|length %} | |
{% set results = results.search(search|join(' ')) %} | |
{% endif %} | |
{# ADD QUERY FILTERS HERE (see other files) #} | |
<form id="filter_form" action="{{ url('blog') }}" class="grid-x grid-margin-x"> | |
<div class="cell medium-6 large-4 xxlarge-3"> | |
<div class="styled-select"> | |
<select name="date"> | |
<option value="" disabled selected>Date</option> | |
<option value="">All</option> | |
<option value="2018" {% if query.date and query.date == '2018' %}selected{% endif %}>2018</option> | |
<option value="2017" {% if query.date and query.date == '2017' %}selected{% endif %}>2017</option> | |
</select> | |
</div> | |
</div> | |
<div class="cell medium-6 large-4 xxlarge-3"> | |
<div class="styled-select"> | |
<select name="category"> | |
<option value="" disabled selected>Category</option> | |
<option value="">All</option> | |
{% for cat in craft.categories.group('category').all() %} | |
<option value="{{ cat.title }}" {% if query.category and query.category == cat.title %}selected{% endif %}>{{ cat.title}}</option> | |
{% endfor %} | |
</select> | |
</div> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
filterForm() { | |
let form = document.getElementById('filter_form'); | |
let selects = form.querySelectorAll('select'); | |
selects.forEach(el => { | |
el.addEventListener('change', e => { | |
e.preventDefault(); | |
form.submit(); | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% if query.category %} | |
{% set search = search|merge(['category:' ~ query.category]) %} | |
{% endif %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# filter the results based on a year being passed. I.e. all blogs in 2018. #} | |
{% if query.date %} | |
{% set results = results | |
.postDate('>= ' ~ query.date) | |
.postDate('< ' ~ (query.date + 1)) | |
%} | |
{% endif %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment