Hello
I build a custom block that renders an HTML <picture>
-element using a large list of images in different sizes and for light and dark mode. I am running into a behavior I don’t understand.
The Problem
What should happen
The code (see below) tests if a field is empty. If the field is empty, it does nothing. If the field is not empty, it renders an HTML <source>
-element linking to the file referenced in the field.
What actually happens
If the field is empty, the code works. If the field references an image, an error message is displayed:
Block error: “Call to undefined function isNotEmpty()” in block type: “image”
How I interpret this
If the field is empty, Kirby correctly evaluates the field to be empty. If the field contains data, Kirby tries to call isNotEmpty()
on the field, which fails. A field that contains data seems not to support testing if it is empty.
The Block / Details
How I structured the block — files
site:
blueprints:
blocks:
image.yml
snippets:
blocks:
image.php
image.php
| Snippet
I shortened this excerpt, it causes the same error.
<picture <?= ($block->class()->isNotEmpty()) ? "class='" . preg_replace("/, /", " ", $block->class()) . "'" : "" ; ?>>
<?php
//Line Below causes error if certain conditions are met:
if($block->imageMobileDark()->isNotEmpty()) :
//Line Below causes error if certain conditions are met:
$srcset = ($block->imageMobileHiDPIDark()-isNotEmpty()) ? $block->imageMobileHiDPIDark()->toFile()->url() . " 3x, " : "";
$srcset .= $block->imageMobileDark()->toFile()->url() . " 1x";
?>
<source media="(max-width: 413px) and (prefers-color-scheme: dark)" srcset="<?= $srcset ?>" />
<?php
endif;
?>
<img
<?= ($block->class()->isNotEmpty()) ? "class='" . preg_replace("/, /", " ", $block->class()) . "'" : "" ; ?>
alt="<?= $block->alt() ?>"
src="<?= $block->imageDesktop()->toFile()->url() ?>" />
</picture>
image.yml
| Blueprint
Also shortened.
name:
de: Bild
en: Image
icon: image
tabs:
content:
fields:
class:
label:
de: Layout-Eigenschaften
en: Layout-Options
type: multiselect
options:
breakout:
de: Über-Breit
en: Hero Image
left:
de: Linksbündig
en: Left aligned
right:
de: Rechtsbündig
en: Right aligned
none:
de: Volle Breite
en: Full Width
floating-illustration:
de: Illustration
en: Illustration
alt:
label:
de: Ersatztext
en: Alt-Text
type: text
imageMobileHiDPIDark:
width: 1/2
label:
de: 390w @3x – Nacht
en: 390w @3x – Night
type: files
uploads: image
min: 0
max: 1
The complete files
Question
Am I doing something wrong? If so: What should I do differently? If not: Is this a bug?