Eps file in structure field

I’ve got a structure field with logo’s, a field with a png and a field with an eps both fields can only contain one file.

I’m outputting the png files as expected, for example the filename:


For the eps file, i get an error:


error: Call to a member function filename() on null

But this gives me an object:


Kirby\Cms\File Object
    [dimensions] => Array
            [width] => 0
            [height] => 0
            [ratio] => 0
            [orientation] => 

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

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

            [timestamp] => 1684231856
            [exposure] => 
            [aperture] => 
            [iso] => 
            [focalLength] => 
            [isColor] => 

    [extension] => eps
    [filename] => s4s-logo.eps

But how can I output the filename for example?

Since you are doing this in a loop (the structure field), is every eps field filled? You should never call an object method (like filename()) without making sure you have an object.

Yes, of course.
I was trying to cut corners :smirk:, thinking there can be only one file in the field, didn’t think of an empty field, though.

This does the trick:

<?php foreach ($item->eps()->toFiles() as $eps) : ?>
  <?= $eps->filename() ?>
<?php endforeach ?>

Well, I wouldn’t recommend this if the field only has one file. The best practice would be (depending on what you are doing here):

if ($file = $item->eps()->toFile()) {
  echo $file->filename();

or using the null-safe operator:

echo $item->eps()->toFile()?->filename();

Ok, thank you. So what I’m doing now is something like:

<?php $items = $page->logos()->toStructure();
   foreach ($items as $item):
      $image = $item->image()->toFile();
      $eps = $item->eps()->toFile();
     <?php if ($image): ?>
        <figure class="<?= $item->classname() ?>">
           <img src="<?= $image->url() ?>" width="<?= $image->width() ?>" height="<?= $image->height() ?>" alt="">
     <?php endif ?>

     <?php if ($eps): ?>
         <?= $eps->filename() ?>
     <?php endif ?>

<?php endforeach ?>