I have a hidden structured data field called favorites
for instance,
favorites:
label: Favorites
type: structure
style: table
modalsize: large
entry:
- airportalid
fields:
airportalid:
label: Airportal Id
type: text
limit: 5
On any given page you can add a favorite or delete a favorite from the page.
I want to create a page that queries all these values across all the pages a type: text might have been saved.
For instance, a type: text value of 85 is saved on 20 pages.
When they go to a page I set a query that shows me all the pages with a value of 85 saved in that .txt files structured data under favorites.
First of all, to get all structure field entries of all subpages:
$collection = page('projects')->children()->visible(); // replace with your page collection
$favorites = new Structure();
$key = 0;
foreach ($collection as $item) {
foreach ($item->favorites()->structure() as $i) {
$features->append($key, $i);
$key++;
}
}
$favorites
now contains all structure field items of all pages in $collection
.
Now you can filter $favorites
by value in the airportalid field:
$value = 85;
$filtered = $favorites->filterBy('airportalid', '==', $value);
// if you want to count them
$count = $filtered->count();
I need to scan the whole site though, not just under one collection. This could be stored in any page file on the whole site.
Then you have to use
$collection = $site->index();
Note that this might get slow with thousands of pages and you might want to cache the result.
How do you return the page url that each value lands on?
Also did you mean favorites here.
$features->append($key, $i);
Sorry, that copied it over from my example collection and overlooked the variable, should of course be $favorites->append($key, $i)
.
You should be able to get the page via the field:
foreach($filtered as $item) {
$pUrl = $item->page()->url();
}
Let me know if that works.
Edit: Looks like that doesn’t work, but this does:
foreach($filtered as $item) {
$pUrl = $item->airportalid()->page()->url();
}
This returns the current pages url in multiples of that value being found. Not the page url of the page which it was found on.
$collection = $site->index();
$favorites = new Structure();
$key = 0;
foreach ($collection as $item) {
foreach ($item->favorites()->structure() as $i) {
$favorites->append($key, $i);
$key++;
}
}
$value = "83";
$filtered = $favorites->filterBy('airportalid', '==', $value);
foreach($filtered as $item) {
$pUrl = $item->page()->url();
echo $pUrl;
}
$count = $filtered->count();
Not quite sure I get what you mean. What page URL do you expect to get? Don’t you want to get the URL for each page that value can be found on? Or do you want to exclude duplicates in case the same value appears multiple times on a single page?
The value will appear on that page only once. It’s more of a unique id.
The issue is that somehow that $item->page()->url(); is returning the url of the current page. But will echo $pUrl as many times as the value is found on the other pages.
So if I go to http://site.com and there are 2 instances of 83. Then it will echo http://site.com 2 times. If I’m on http://site2.com and there are 2 instances, the same that were searched on site.com. The it will echo http://site2.com 2 times as well. Instead of saying this exists once on site.com and once on site2.com. Which ever page it’s on it will echo the same instance both times.
Kirby has the best support in the world. My job would be so much more difficult without this wonderful support. It worked. Thank you.
$pUrl = $item->airportalid()->page()->url();
Fixed the issue.
1 Like