cbtr
October 19, 2022, 12:16pm
1
I’m trying to filter a set of pages based on a value from its structure. But I can’t get pass the error:
Array to string conversion
This is my controller for the page location.php
:
<?php
return function ($page) {
// location unique ID
$currentID = $page->content()->uuid()->value();
// Films from season1 filterBy related location
$films = page('programmation/saison1')->children()->listed()->filter(function ($child) use($currentID){
if($child->practical_information()->toStructure()->filterBy('location', '*=', $currentID)) return $child;
});
return [
'films' => $films
];
};
Any advice would be highly appreciated!
$films = page('programmation/saison1')->children()->listed()->filter(fn ($child) => $child->practical_information()->toStructure()->filterBy('location', '*=', $currentID)->count() > 0);
cbtr
October 21, 2022, 6:29am
3
Thank you for your reply. I tried it but same error. The issue seems that when using the uuid in a field and filtering it with the ->filterBy
, kirby reads the uuid as a page object instead of a string value. So what I did currently, is to force, the structure into an array with ->yaml()
and then search with array_column
and in_array
.
Not ideal but it works.
$films = $films->filter(function ($child) use ($page) {
$yaml = $child->practical_information()->yaml();
return in_array($page->uuid(), array_column($yaml, 'location'));
});