Accessing data from a different $page that is no child

Hi there,

I’m know how to use data from page <?= $page->application_headline() ?> and I also found how to use data from children. But I haven’t found a good explanation on how to use data from a different directory.

I have a page called references that has subpages (subfolders) for each reference. (very similar to your projetcs/project example from the tutorial) I have product pages were I would show these refences. I already created a blueprint that allows to select and link the reference. Now I need to add access the data from the template. How can I do this? I read something about URI but didn’t understood how to use it.

I would appreciate a little hint.

The product pages and refenrences are on the same level (siblings)

Could you please show us the product blueprint where you select the reference?

Hi Sonja.

this!?

  - width: 1/1
        fields:
            testimonial_2:
                label: Testimonial
                type: pages
                template: reference
                max: 1
                required: true

You can get the page object from this field like this:

if ( $reference = $page->testimonial_2()->toPage() ) {
  echo $reference->title(); // or whatever you want to output from that page
}

One second. I will try this immediately! :slight_smile:

Working!!!

->toPage( ) was the key.

You are very kind, knowledgable and fast Sonja. Many thanks. Pretty impressed by your skills!

Best
Marc

1 Like

@marc22761 Welcome to our community!

Thanks. I really like Kirby and the support is playing an important role in this.

Two more questions:

When I click “add” at the references page in the panel, I only get the option to select an existing page, not to create a new one.

references.yml

Title: References

columns:
    - width: 1/1
        sections:
            references:
                type: fields
                fields:
                    references:
                        label: References
                        type: pages
                        template: reference

reference.yml

Title: Reference

columns:
    - width: 3/4
        sections:
            reference:
                type: fields
                fields:
                    category:
                        label: Category
                        type: tags
                        accept: options
                        options: [soil, invasive plant, algae & moss, general]
                        min: 1
                        required: true
                        width: 1/2
                    company_country:
                        label: Country
                        type: text
                        required: true
                        width: 1/2
                    company_name:
                        label: Company name
                        type: text
                        required: true
                        width: 1/2
                    company_url:
                        label: Company Url
                        type: url
                        width: 1/2

Second question:

The ->toPage() solved my problem with all text elements. One of them is a picture.

   fields:
                    picture:
                        type: files
                        label: Reference person picture
                        layout: cards
                        size: small
                        multiple: false
                        template: image
                        image:
                            ratio: 1/1
                            cover: true
                            required: true

Template →

Result:
which is obviously not a valid src path. I tried echo $reference->picture()->url() with the same result.

In your references.yml, you use a pages field. instead of a pages section. A pages field is for selecting pages, not for adding new pages. So you would have to change this into a section.

Here you have to use toFile() to convert the field value to a file object:

<?php if ( $image = $page->picture()->toFile() ) : ?>
  <img src="<?= $image->url() ?>" alt="">
<?php endif; ?>

If you use three backticks (not quotes etc.) before and after a block of code, it will be highlighted and become readable.

To use a section instead of a field was the key. Adding is now working. Unfortunately I wasn’t able to make the image work with ->toFile()

I think it is because the image is in a different directory and therefore missing the path.

I have the following entry in my page.txt

- references/Wouter-Greens

In the reference.txt in the Wouter-Greens folder it has

Picture:

- ens_wouters_green_12.jpg

In the template:

<div class="tt-image">
                    <img src="<?php if ( $reference = $page->testimonial_1()->toFile() ) {
                        echo $reference->picture()->url(); } ?>" class="testimonial-img">
                    </div>
                    <div class="tt-statement">
                        <div class="italic normal-font testimonial-statement-text"><?php echo $reference->statement(); ?><a href="#">Read more</a></div>
                    </div>
                    <div class="tt-credits">
                        <h6 class="medium"><?php echo $reference->reference_name(); ?></h6>
                        <h6><?php echo $reference->reference_role(); ?></h6>
                        <h6><?php echo $reference->company_name(); ?>, <?php echo $reference->company_country(); ?></h6>
                    </div>

Everything in tt-statement and tt-credits is working. tt-image is not returning a valid url. I believe I somehow need to combine toPage and toFile in order to retrieve the url but I can’t make it work.

I would appreciate a little help. Thank You!

This code I don’t understand. What type of field is testimonial_1? If that is a page reference, then you have to use toPage() rather than toFile().

That is what I meant when I wrote I think I have to combine toPage() and toFile().

I’m trying to describe what I’m trying to achieve. I have a couple of references that I would like to link to a page (the reference to show on the product page can be selected in the panel) rather than hard coding it into the page. I created the folder content/references and in there subfolders for each Reference (e.g. contant/references/wouter-greens). The image for this reference is in the subfolder → /content/references/wouter-greens/ens_wouters_green_12.jpg

My product page from were I’m trying to access the picture is /content/product

In product.yml I defined testimonial_1

 - width: 1/1
        fields:
            testimonial_1:
                label: Testimonial
                type: pages
                template: reference
                max: 1
                required: true

This allows me to select the testimonial that I want to show on the product page. I can change the testimonial and its showing the selected statement and information on the product page (thanks to your ->toPage() hint). Except the picture.

For my understanding I probably have to connect to the reference first with ->toPage() and retrieve the url to the image afterwards with ->toFile(). But I’m not 100% sure how to do it and all attempts were unsuccessful so far.

Yes, exactly.

$reference = $page->testimonial_1()->toPage();

This gives you the page object from the value that is stored in the testimonial_1 field.

Now for the image which is stored in the reference page in the picture field, so here we first check if the page exists and then we try to convert the value in the picture field to a file object

<?php if ($reference && ( $image = $reference->picture()->toFile() ) ) : ?>
  <img src="<?= $image->url() ?> class="testimonial-img">
<?php endif; ?>

Working. Solved! Thank You!!