Query parent images and get URLs in template files

If you’re in the same situation as me, consider a simpler approach as mentioned by texnixe below.

I’ve got the following setup.

This is the parent page and it’s image field:

  label: Header image
  type: image
  width: 1/2

Where the user chooses one of the uploaded images.

By default, I want the child pages to use that very same image, unless they have an uploaded image on their own. So my first thought was this:

  label: Header image
  type: select 
  options: query
    page: ../
    fetch: files
    value: '{{filename}}'
    text: '{{filename}}'
  width: 1/2

The value is being set just fine - parentimage.jpg. However I’m struggling with getting the proper url.
The URL for that image should be something like site.com/parent/parentimage.jpg but the URL I’m actually getting is site.com/parentimage.jpg.

So, my two questions are:

  1. How can I get the child header_image field to default to the same as it’s parent? Right not it’s empty until manually selected.
  2. Which method should I use in order to get the proper url? I’ve tried a few combinations of toFile() and url() but so far they’ve come up short.

I’d choose another approach. In the child page, let the user select an image from that page only. Only if this field is empty, get the image from the parent field.

Definately think your right. After not looking at this for a while I definitely need to simplify this. Might mean more work as of right now, but in the long term it’ll be much smoother.

In any case, I still struggle to understand why I couldn’t get the URL to work properly. Do you have any ideas as to why?

To get the proper URL:

if($image = $page->parent()->image($page->header_image())) {
  echo $image->url();

toFile() only works with images from the current page.

1 Like

Oh, well that explains it! Thanks!