Get specific file from multiple file fields

Hi there! I’m running into an issue with fields when I try to get a specific file.

In my blueprint I have a field for an image (cover) which is used as a header image on the site.
And I have a field for an icon (icon), which is used on a different page where I list the pages.

I want to use the cover but if no cover is uploaded I want to display the snippet header (no image, just headline). At the moment the Icon (SVG) is displayed when there is no cover uploaded. I don’t understand why because I used <?php if ($cover = $page->cover()): ?>.

Can anyone help?

My blueprint:

sections:
      meta:
        type: fields
        fields:
          cover: fields/cover
          icon:
          	type: files
          	accept: image/svg+xml

Content auf fields/cover:

type: files
multiple: false
query: page.images.template('image')
uploads:
  template: image

My template:

<?php if ($cover = $page->cover()): ?>
  <section class="container-fluid stage">
    <div class="row">
      <div class="col-12 image">
        <style>
          .stage .image { background: no-repeat center url(<?= $cover->url() ?>); background-size: cover; }
        </style>
      </div>
    </div>
  </section>
<?php else: ?>
  <?php snippet('intro') ?>
<?php endif ?>

This should read:

<?php if ($cover = $page->cover()->toFile()): ?>

because you have to convert the field content to a file object first.

On a side note, the accept property here won’t have any effect, it has to be set in the file blueprint used for this field.

Thank you for your quick reply!
When I do this no image is loaded, but the <?php else: ?> doesn’t work, so the snippet isn’t loaded instead. :frowning:

When you use your original code and do a

dump($cover);

what is the result?

What do you get instead? An error? Nothing at all?
Could you post the code in the snippet?

The dump is

Kirby\Cms\File Object
(
    [root] => /www/htdocs/xxx/content/1_leistungen/20210505_elektromobilitaet/elektromobilitaet.svg
    [hash] => ae88833fabe6153621226c1d0ea4b725
    [filename] => elektromobilitaet.svg
    [name] => elektromobilitaet
    [safeName] => elektromobilitaet
    [extension] => svg
    [size] => 903
    [niceSize] => 903 B
    [modified] => 2021-05-23T09:29:38+02:00
    [mime] => image/svg+xml
    [type] => image
    [isWritable] => 1
    [isReadable] => 1
    [dimensions] => Array
        (
            [width] => 48
            [height] => 48
            [ratio] => 1
            [orientation] => square
        )

    [exif] => Array
        (
            [camera] => Array
                (
                    [make] => 
                    [model] => 
                )

            [location] => Array
                (
                    [lat] => 
                    [lng] => 
                )

            [timestamp] => 1621754978
            [exposure] => 
            [aperture] => 
            [iso] => 
            [focalLength] => 
            [isColor] => 
        )

    [id] => leistungen/elektromobilitaet/elektromobilitaet.svg
    [template] => 
    [url] => http://xxx/media/pages/leistungen/elektromobilitaet/34a169fac6-1621754978/elektromobilitaet.svg
    [content] => Kirby\Cms\Content Object
        (
        )

    [siblings] => Kirby\Cms\Files Object
        (
            [0] => leistungen/elektromobilitaet/elektromobilitaet.svg
            [1] => leistungen/elektromobilitaet/stromschmiede_elektroinstallation.jpg
        )

)

But I get no errors.

In the snippet intro.php there is:

<section class="container-fluid head">
  <div class="row fluid-spacer">
    <div class="col-12">
      <article>
        <h1 class="h1"><?= $page->title()->html() ?></h1>
      </article>
    </div>
  </div>
</section>

Ok, then this obviously already gives you a file object without converting to file which means that you are probably using the code from the Starterkit, where the cover() method is defined in the page model album.php or note.php. This model falls back to the first image in the page if the cover file doesn’t exist:

  public function cover()
    {
        return $this->content()->get('cover')->toFile() ?? $this->image();
    }

This is true. Should I remove these models ?

Either remove it (and then call toFile() on the field as suggested above), or remove the fallback, so that it looks like this:

public function cover()
{
     return $this->content()->get('cover')->toFile();
}

I did remove the fallback and changed back to

<?php if ($cover = $page->cover()): ?>
[...]
<?php else: ?>
<?php snippet('header') ?>
<?php endif ?>

but it still shows the svg :-/

Should I try another approach in the blueprint without “cover”? As u said I used the starterkit and modified notes for my purpose.

Thank you so far. I’m still learning things :slight_smile:

Ah, I found another model containing the fallback. Now that I removed it, it works as you said.

Thank you very much!! <3