I have a list of roles I would like to show in a select field. The problem is I want to only show roles that have results above a certain number. How would i do this inside a blueprint file?
OK so each post can have two tags, but rather than listing all tags I only want to list the tags that appear in posts over a certain number of time. Only show this tag if when used as a filter will show more than 5 results.
Iām building an interview website and on the homepage I want to give admins the ability to choose a filter based on location or role of the interviewee.
The problem I am now facing is that some locations and roles only have 1 or 2 interviews. I want to exclude the roles and locations from the dropdown in the Panel that would show less than 5 results.
Iāve been able to create Page Methods that make it easy to access the locations and roles but now I want to filter them down to only show roles that will output 5 or more results.
Thatās not as straightforward as expected, because in this case, your pages do not even contains the saved string.
So basically you would have to create either a new method, or inside this method, a way to filter the locations by separating that string again into city and country and check which of the pages has both values in the yaml array.
You could fetch your locations from a collection. Doesnāt create much overhead but gives you the full power of php.
Havenāt tested it, but I think this could work
/site/collections/cities.php:
<?php
return function ($site) {
return $site
->find('interviews')
->children()
->groupBy('location') //create a collection of collections
->filterBy('count', '>=', 5) //use only those that have at least 5 pages
->map(function($collection) {
//map each collection to the location of it's first page (it doesn't
//actually matter which page, they all have the same location)
return $collection->first()->location();
})
};
Hey, thanks so much for chiming in. So I played around with this but ran into a few issues.
I donāt think the groupBy would work as ālocationā isnāt actually a field. mymap is a structure field. I created a page method to pull out the location (city & country). What would be the way around this?
Mymap:
lat: 42.3602534
lon: -71.0582912
city: Boston
country: United States of America
----
I tested this with the role as this is a field that is available. The query seems to work but I get this error. It looks like it wants me to define the text and value bit I donāt know what to place there.
Should definitely work, the groupBy function doesnāt distinguish between āreal fieldsā and page methods (or any method for that matter). You just have to make sure every page you want to group has data in the myMap field, otherwise your ālocationā method returns null, and that canāt be used as a group key.
Itās just kirby being weird: it seems like, for the panel, the query result canāt be a Collection of strings (which is what we get out of our āmapā call). On the other hand it definitely accepts an array of strings. So instead of calling āmapā, we need to call ātoArrayā.
Hereās the function:
<?php
return function ($site) {
return $site
->find('interviews')
->children()
->filterBy('location', '!=', null) //NEW: consider only pages with locations
->groupBy('location') //create a collection of collections
->filterBy('count', '>=', 5) //use only those that have at least 5 pages
->toArray(function($collection) { //NEW: toArray, not map
//map each collection to the location of it's first page (it doesn't
//actually matter which page, they all have the same location)
return $collection->first()->location();
});
};