Second 'cover' in my yml producing incomplete img url

I’m using this cover.yml blueprint

# Fields can be defined in their own field blueprints
# and reused across multiple blueprints
# This field is used in the album.yml and note.yml blueprints
# See http://getkirby.com/docs/guide/blueprints/extending-blueprints#reusing-and-extending-single-fields
type: files
multiple: false
query: page.images.template('image')
uploads:
  template: image

I’m adding two ‘covers’ to the top of my page blueprint

fields:
  # If you need the same field in multiple locations, you can define them once and reuse whereever needed.
  # Here we use a files field defined in `/site/blueprints/field/cover.yml`
  cover: fields/cover
  cover2: fields/cover

I’ve populated the page with two different images via the panel.

Loading the images into the page with this php (cover, then cover2):

<?php if ($cover = $page->cover()): ?>
<img src="<?= $cover->resize(800, 800)->url() ?>" alt="<?= $cover->alt()->esc() ?>">
<?php endif ?>

<?php if ($cover2 = $page->cover2()): ?>
<img src="<?= $cover2->resize(800, 800)->url() ?>" alt="<?= $cover2->alt()->esc() ?>">
<?php endif ?>

the first image loads

the second ‘cover’ always produces an invalid/incomplete url

src=“- ‘file://g8kawccqej5alpmf’”

If I swap them around (cover/cover2) in the php, or in the xml , it always feels like it’s ‘cover2’ that never resolves to the full url. I’ve tried ->toFile(), and ->image() with subsequent errors. And nothing I try seems to solve this. The image files are in my page folder. The uuid’s match in their .txt files. I’ve changed images.

The panel on the other hand, can display the both images from the page’s source folder.

What am I missing?

You’re missing toFile():

<?php if ($cover = $page->cover()->toFile()): ?>
<img src="<?= $cover->resize(800, 800)->url() ?>" alt="<?= $cover->alt()->esc() ?>">
<?php endif ?>

<?php if ($cover2 = $page->cover2()->toFile()): ?>
<img src="<?= $cover2->resize(800, 800)->url() ?>" alt="<?= $cover2->alt()->esc() ?>">
<?php endif ?>

If you have a cover() page method (starterkit?) you want to do this instead:

<?php if ($cover = $page->content()->cover()->toFile()): ?>
<img src="<?= $cover->resize(800, 800)->url() ?>" alt="<?= $cover->alt()->esc() ?>">
<?php endif ?>

<?php if ($cover2 = $page->content()->cover2()->toFile()): ?>
<img src="<?= $cover2->resize(800, 800)->url() ?>" alt="<?= $cover2->alt()->esc() ?>">
<?php endif ?>

…or just get rid of the cover() method as it doesn’t really make sense with two covers.

since you wrote that you’ve tried it with the toFile() method, I would suggest to check/deactivating the page cache/php cache, depending on your setup :slight_smile:

I turned off caching in the config. And removed the site cache. I’m not sure whether I also need to restart kirby in some way? But I see no change.

Also, is there a separate process for clearing php cache?

With regards ->toFile() :

This is interesting. As I said I tried this before, but perhaps only tried it on the first ‘cover’.

What happens is this:

Using ->toFile() breaks the first cover.

Using ->toFile() fixes the second cover.

It’s a fix – but isn’t this a bit strange? I’m happy to use this, but it would be nice to understand what’s going on, to know whether this fix is reliable.

‘Stop using the second cover’.

Yes that’s an option I have not tried. It would be interesting to see what happens.

Yeah, that’s exactly what I meant. Looking at the starterkit source code, I can see there’s a cover() function defined in the AlbumPage model: starterkit/site/models/album.php at 122ed448d8e12c066e151c778122f76b083fd154 · getkirby/starterkit · GitHub

So calling $page->cover() actually already returns an image (look at the toFile() there). Calling toFile() again breaks it. There is no cover2() function defined, that’s why they are different.

In my humble opinion this is a bit confusing and I would call the function coverImage() to not be confused with the cover field.

cover2: fields/cover

cover2 is defined as just another fields/cover. cover2 is just a variable name. So they (cover and cover2) should behave the same way?

In the blueprint, yes. But then in the model there’s a custom cover() function that overrides what $page->cover() returns. There’s none for $page->cover2().

I kind of see what you mean. But I’m still confused – I thought ->cover2() returned the page instance of ‘cover2’ defined in the blueprint as a field/cover?

So. I need to rename my variables? To prevent any confusion? I’ll do that now and see what happens. Thanks.

One thing you’ll have to understand is that Kirby doesn’t give a damn about your blueprints in the frontend. It only looks at the content files and that’s why I always suggest doing the same. Look at the .txt files and then you see what you’re working with.

At this point the custom cover() function is just getting in your way so I’d suggest simply deleting site/models/album.php. Then you can access both fields the same way, using toFile() to turn them into files.

I’m not quite sure what you mean about deleting albums.php, but you are right – calling my variable the same name as the blueprint defined custom field type was a big mistake. (Of course now I see that originally I was using an upper case C in ‘Cover’, to distinguish it from the field type definition. But then I forgot to do that in the php code- and things got more confused from there on.)

So – as you earlier suggested - I called them coverImage and coverImage2, and then all the problems went way. And using ->toFile(), of course.

Many thanks for your help and patience. I now feel like I’m back on track.