I would like to have a ‘select’ dropdown field in the frontend that is populated by only selected options from the content file, not all options from the blueprint.
Oh, sorry, that’s a different story. But @pixelijn is right, pluck could do this. In a custom method, you would pluck all values from the pages and push them into an array which you then return.
Although this works for my specific case, how would I go about making it more reusable? At the moment it looks for specific fields by name, is there a way I can get it to just look for a select field within a structure regardless of its name? @pixelijn@bruno
The content files don’t store information about their content “type”. For that you’d have to load the blueprint and work through that. (like pixelijn wrote)
You could however add some attributes to your method and tell it what fields it should use:
<?php
Kirby::plugin('my/pagesMethods', [
'pagesMethods' => [
'structurePluck' => function ($structure, $field) {
$values = [];
foreach($this as $page) { // $this refers to the $pages collection you are calling the method on
$value = $page->{$structure}()->toStructure()->pluck($field,',',true);
array_push($values, $value);
}
return $values;
}
]
]);
use it like this: $districts = $places->structurePluck('location', 'district');
I tried to include the array_flatten function in the pagesMethod itself, but this doesn’t seem to work. Storing it in the controller works, but I may want to use this on multiple pages and don’t want to have to add the function to every controller that uses structurePluck.
Where can I store this function so it’s more reusable?