Problem getting kirby-builder to work with kirby3-srcset

Hi

I’m having some issues getting kirby-builder to work with kirby3-srcset.

I have a builder block with the following field:

selectedimage:
  label: Image
  type: files
  max: 1
  layout: list
  size: tiny
  multiple: false

And i try to fetch the data like this from the appropriate builder blueprint:

<?= $data->selectedImage()->toFile()->lazysrcset(); ?>

But it won’t return anything but this error:

Argument 2 passed to Bnomei\Srcset::__construct() must be of the type array, null given, called in /www/site/plugins/kirby3-srcset/index.php on line 21

It works fine if i use the image() function instead of the lazysrcset();

It’s probably me missing something super basic here, but any help would be greatly appreciated

maybe some if your data does not have an image yet. make sure to check if the field is empty before trying to call the lazysrcset fieldmethod.

if($data->selectedImage()->isNotEmpty()){...}

Or better (because if the field is not empty doesn’t mean you get a file object):

<?php if ($image = $data->selectedImage()->toFile()) : ?>
<?= $image->lazysrcset(); ?>
<?php endif ?>

@bnomei I actually get the same error, your constructor in /classes/Bnomei/Srcset.php expects an array, so if I pass an empty array or a string as argument to the file method, it works. But the constructor doesn’t cover the case null is passed.

I created an issue on GitHub.

1 Like

thank for the reply, @texnixe and @bnomei

Unfortunately neither solution helps. If i do a var_dump on the

$data->selectedImage()

I get this:

object(Kirby\Cms\Field)#407 (1) { [“selectedimage”]=> array(1) { [0]=> string(14) “untitled-4.png” } }

Does that help?

As I wrote, there’s a bug, try this:

<?php if ($image = $data->selectedImage()->toFile()) : ?>
<?= $image->lazysrcset([]); ?>
<?php endif ?>
1 Like

Thank you so much, that fixes it :slight_smile:

Have a nice day!