Hi,
I have a website that has some projects. Each project page can have one or multiple authors which are also pages. These pages can be selected in the project panel page.
I would like to make the editors life a bit easier by then also displaying the related projects in the author page. These would have to be queried dynamically. In the frontend I fetch them like this but I don’t know how or if I can do this in the panel.
<?php $projects = $site->find("projects")->children()->filter(function ($child) use ($page) {
return $child->author()->toPage()->is($page);
}) ?>
Thank you!
You need a page method or page model method that you can then use within your pages section query.
Could you please provide me with an example? I didn’t find any information how I could use a page method to query something in the blueprint. Thank you!!
Since you cannot use that rather complex query, you need to move it to some method, ideally in a model for the author page.
Your section in the author.yml
pages:
type: pages
query: page.getProjectsForAuthor()
The in a model for the author page (site/models/author.php, provided those author page use an author.yml blueprint:
class AuthorPage extends Page
{
public function getProjectsForAuthor()
{
return page('projects')->children()->listed()->filter(fn($child) => $child->authors()->toPages()->has($this));
}
}
1 Like
Ahh, I didn’t know I could use methods or fields in a blueprint query like that. Thank you!!