Field + isNotEmpty()?

Is there a good way of checking if my picture() field is not empty?
Can’t get the isNotEmpty() function to work correctly.

<?php foreach($page->builder()->toStructure() as $subpage): ?>
  <?php if($s = $subpage->picture()->isNotEmpty()): ?>
{
                  "img": "<?= $s->url() ?>", 
}				  
  <?php endif ?>
<?php endforeach ?>

You syntax is not correct, at least isNotEmpty() only returns a boolean and you can’t call the url() method on a boolean.

<?php foreach($page->builder()->toStructure() as $subpage): ?>
  <?php if($img = $subpage->picture()->toFile()): ?>
   <img src="<?= $img->url() ?>" >	  
  <?php endif ?>
<?php endforeach ?>

It is not necessary to check if the field is empty or not, we need a file object here; and if we call the toFile() method we either get a file object if the image exists, or we get a boolean false if the field contains a value for an image that doesn’t exist (anymore) or if the field is empty.

Also, you can’t have curly braces outside of PHP code, I don’t know what you are trying to do there.

1 Like

thank you! toFile() method works perfectly.