but I don’t know how to get all the pages first ! This is silly.
And I have the feeling that something easier could be done. With $page->evaluate(…) maybe ?
Thank you for any tip. I will also appreciate any relevant link to the documentation.
P.S: A convenient solution could be to make this “location” field a part of the taxonomy. But it bother me that this taxonomy field will appear in the admin settings form, for every page, including the pages where this field make no sense.
public function onPagesInitialized()
{
$city = 'New York';
$pages = $this->grav['pages']->all();
$pages->filter(function ($slug, $path) use ($pages, $city) {
$page = $pages[$path];
$header = (array) $page->header();
if (!empty($header['news']['location'])) {
$location = $header['news']['location'];
return (is_array($location) && in_array($city, $location)) ||
$location === $city;
}
return false;
});
// Process found pages
}
Use taxonomy as you suggested.
To prevent taxonomy field from being displayed on ‘default’ pages you could override the ‘default’ blueprint and remove the taxonomy field .
Docs: Removing Fields / Properties (unset-*@)
Create a dedicated blueprint for your own custom page type/template and add the taxonomy field there together with other fields required for your page header (e.g ‘link’) in a dedicated ‘tab’ that only shows in that page type.
Docs: Example page blueprint
Yes indeed ! Thank you very much for this quick and really complete answer. You make me understand grav better.
This code works fine (and the empty() test is a good shortcut).
I’m already making a blueprint for this type of pages, but after some reading here, it seems tricky to me to hide and displace only one taxonomy field. I think I will stay with the first solution.