Access data of related content

I apologize in advance if this is a total noob question and the answer should be obvious. I promise I searched thoroughly, I’m seriously stuck and would appreciate any nudge or hint in the right direction.

This is what I’m working with (example)

- Home (standard page)
- Projects
    - Project
- Galleries
    - Gallery
- …

On standard pages, the editor can choose one (and only one) project to be featured on this page. This works nicely:

title: Standard Page
pages: true
files: true
    label: Title
    type:  text
    label: Text
    type:  textarea
    label: Project
    type: select
    options: query
      fetch: pages
      template: project
      value: '{{uid}}'

This is what I am trying to to:

„Project“-pages have a file field for a teaser image. I want to display title, teaser and teaser image of the selected project on the standard page.

title: Project Page
pages: false
    label: Title
    type:  text
    label: Teaser
    type:  textarea
    label: Teaser Image
    type:  selector
    mode:  single
    filter: /\.((png)|(jpe?g))/i
    autoselect: first
        - image

I can print the title nicely with the following function in the “standard page”-template:

<?php echo $page->featuredproject()->toPage()->title(); ?>

But only as long, as the project page is a sibling of the standard page. As soon as the project becomes a child of „projects“, the title is empty and I can’t figure out how to access the data and how to print the field content like „teaser“ and the image url.

Could someone please point me in the right direction?

Well, I think you can achieve this with a ‘page’ field type in your standard page blueprint. With this field, the editor can find a page (or in this case, featured project), by typing in the title of the project (let’s say: project/your_project)*.

Check the docs:

You can do something like this then:

// This will find the page by the value of the page fieldtype in your standard page :)
$featuredProject = $pages->find( $page->your_page_field() );

// Since the $featuredProject variable holds a Page Object, we can access all the data now :)
echo $featuredProject->title()->html();
echo $featuredProject->teaser()->kirbytext();
echo $featuredProject->teaser_image()->url();

*If someone knows how to limit the scope of the ‘page’ fieldtype to only a certain page-type (in this case /projects/ for example), it will be even easier for the editors!

@gerardkd This will not work, because $pages only searches within the pages of the first level.

Instead, you can use:

$featuredProject = page('projects/' . $page->featuredproject());


 $featuredProject = page('projects')->children()->find($page->featured project());

Whow, thank you for your superfast replies!!

The first solution is an alternative to the part that is already working: I can already access the desired fields, but only as long as the project and the referencing page are siblings.

Dang, I figured that much and that explains it.

texnixe’s suggestions work, but I was very much hoping to find a more dynamic approach. So there is no way to get the full path to the linked project? I definitely need to provide the parent?

Thank you for helping me finding my way around Kirby! it is much appreciated!

Well, you could use $site->index()->find($page->featuredproject())

Ahhh, sometimes I’m just blind. It was a tough birth, but it finally sunk in, thank you so much!!

This is what I came up with, I hope it’s the Kirby way to go…

// get the selected project 
$selectedProject = $page->featuredproject()->value();
// get all pages of type „project“, returns $pages (not required but I like going step by step)
$allProjects = $site->index()->filterBy('template', 'project');
// search in all projects for the selected project and return $page
$featuredProject = $allProjects->findByURI($selectedProject);

Now I can dig into $featuredProject:

echo $featuredProject->title();
echo $featuredProject->fieldname();

$teaserImage = $featuredProject->teaserImage()->toFile(); 
echo $teaserImage ->url();

Thank you once again for your support!