Saved image unavailable in template

I have set up the following blueprint and want to output the URL of the single image saved via the panel. However, there is no output. All text fields work properly but there’s no output for the filename.

title: News

columns:
  main:
    width: 2/3
    sections:
      description:
        type: fields
        fields:
          text:
            type: textarea
            size: large
            required: true
  sidebar:
    width: 1/3
    sections:
      meta:
        type: fields
        fields:
          date:
            label:
              en: Date
              de: Datum
            type: date
            time: true
            default: today
            required: true
      cover:
        type: files
        headline:
          en: Image
          de: Bild
        layout: cards
        template: news
        uploads: news
        max: 1
        info: "{{ file.dimensions }}"
        image:
          cover: true

According to the documentation

echo $page->cover()->toFile()->url();

should do it but the output is empty. I do not understand what it is that I’m doing wrong. Maybe someone can shine a little light on why this is and what I need to do to make it work.

The code you use is not recommended because you always have to check for an object:

if ( $image = $page->cover()->toFile() ) {
  echo $image->url();
}

Thanks for pointing that out but this of course still outputs nothing since the result is empty. I’d like to know why that is and how to rectify it. Any ideas?

I remember I had a similar thing once, I don’t remember what it was exactly, might be a different thing but try renaming the cover field to something else like coverImage

Sadly this does not do anything.

What I’ve noticed is that the image reference is not reflected in the relevant content text file:

Title: This is the title.

----

Text:

This is some text.

----

Date: 2021-01-19 13:20

In the panel, the image is saved and displayed fine but does not appear in the file. This is probably not expected behaviour. I’m at a loss.

You use a section instead of a field and a section doesn’t store anything in content

You could nevertheless fetch this image by template

if ( $image = $page->files()->template('news')->first() ) {
  echo $image->url();
}

Or you convert your section into a field.

Thanks. I’d rather do it the correct way, which is probably converting the section into a field.

Would you mind explaining a bit more about this? I couldn’t find anything concrete in the documentation explaining in plain English some of those simple use cases. Most samples appear to use presets and modify certain aspects instead of demonstrating a full page blueprint with columns. I always learn better by example and by understanding the underlying concepts.

You can find quite a few annotated blueprint examples in the Starterkit. There are also example here: Samples | Kirby

Difference between fields and sections : Kirby in a nutshell | Kirby

Your sidebar setup currently looks like this (shortened)

  sidebar:
    width: 1/3
    sections:
      meta:
        type: fields
        fields:
          #... fields here
      cover:
        type: files
        headline:
          en: Image
          de: Bild

So under sections you have meta and cover, which makes them both sections.

If you indent your cover section to move it inside the fields section, you have a field

  sidebar:
    width: 1/3
    sections:
      meta:
        type: fields
        fields:
          # ... fields here
          cover:
            type: files
            label: # a field has a label, not a headline
              en: Image
              de: Bild

We also have to rename headline to label for a field.

That’s not so much a matter of one thing being more correct than the other, just different things for different use cases.

Think of the files/pages/users fields as specialized (multi)select fields.

Fantastic, thank you. Your hint regarding converting the section to a field put me on the right track. Good to see your addition confirm my current solution. I’m still confused about sections vs. fields, regarding why I would ever need/want to use a section.

The inner workings of Kirby are confusing in some places, yet I get the impression it’s that way to allow for extreme flexibility. Which is probably useful for advanced solutions but hard to get started with. I’m slowly chewing myself through…

1 Like