Last active
July 18, 2024 11:15
-
-
Save shoesforindustry/5046322 to your computer and use it in GitHub Desktop.
Kirby CMS Multi Filter based on FilterBy with form selection. The idea was to be able to add fields to pages and be able to have a combined multi-filter to select pages which match *all* the filters. Select your filters and then hit the 'Filter' button: A list of selected filters will be displayed and if there are matches the pages names are dis…
This file contains 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
<?php | |
#Pages have these fields, they can have comma separated values for group, category and option | |
# Title: Event1 | |
# ---- | |
# Group: Group1 | |
# ---- | |
# Category: Cat2 | |
# ---- | |
# Option: Option1 | |
# ---- | |
# Text: Some text | |
# ---- | |
# Get the tagclouds, we are using three: groups, categories and options | |
# You also of course need the tagcloud plugin, but just as it is no hacks or duplicates. | |
$items = $pages->find('events'); | |
$groups = tagcloud($items, array('field' => 'group', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
$categories = tagcloud($items, array('field' => 'category', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
$options = tagcloud($items, array('field' => 'option', | |
'sort' => 'name', | |
'sortdir' => 'asc')); | |
# Next get the individual unique names from the tagclouds for the form select filter options | |
?> | |
<form action="" method="post"> | |
<h3>Select Group</h3> | |
<label for="groups">Select Group:</label> | |
<select name="groups"> | |
<?php foreach($groups as $group): ?> | |
<option value="<?php echo $group->name()?>" | |
<?php if(($_POST) and (strval($group->name()) == $_POST['groups'])) echo ' selected ="selected" ';?>> | |
<?php echo $group->name()?></option> | |
<?php endforeach ?> | |
</select> | |
<hr/> | |
<label for="categories">Select Category:</label> | |
<select name="categories"> | |
<?php foreach($categories as $category): ?> | |
<option value="<?php echo $category->name()?>" | |
<?php if(($_POST) and (strval($category->name()) == $_POST['categories'])) echo ' selected ="selected" ';?>> | |
<?php echo $category->name()?> | |
</option> | |
<?php endforeach ?> | |
</select> | |
<hr/> | |
<label for="options">Select Option:</label> | |
<select name="options"> | |
<?php foreach($options as $option): ?> | |
<option value="<?php echo $option->name()?>" | |
<?php if(($_POST) and (strval($option->name()) == $_POST['options'])) echo ' selected ="selected" ';?>> | |
<?php echo $option->name()?></option> | |
<?php endforeach ?> | |
</select> | |
<br/> | |
<button type="submit" id="submit" name="submit" value="Submit" class="btn">Filter</button> | |
</form> | |
<hr/> | |
<h3>Selected filters:</h3> | |
<?php | |
# Check to see if anything has been selected, if so then show the filters selected | |
if ($_POST){ | |
echo '<ul><li>'.$_POST['groups'].'</li>'; | |
echo '<li>'.$_POST['categories'].'</li>'; | |
echo '<li>'.$_POST['options'].'</li></ul><hr/><br/>'; | |
$filtered = $pages->find('events') | |
->children() | |
->filterBy('group', '*=', $_POST['groups']) | |
->filterBy('category', '*=', $_POST['categories']) | |
->filterBy('option', '*=', $_POST['options']); | |
# And then display pages that match the filter if any - you can use anything in the array | |
if ($filtered <>''){ | |
echo '<h3>Pages that match filters:</h3>'; | |
echo '<ul>'; | |
foreach($filtered as $page) echo '<li> <a href="'.$page->url().'">'.$page->title().'</a></li>'; | |
echo '</ul>'; | |
} | |
else #No Pages Match Filter | |
{ | |
echo '<h3>No pages match filters</h3>'; | |
} | |
} | |
else #No Filters Selected | |
{ | |
echo '<p>No filter selected</p>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment