New editor: Referencing image metadata

Hi there,

In the snippet for the image block (snippets/image.php) I can see the following code which “pulls” data from any given attribute of the block:

<img src="<?= $src ?>" alt="<?= $attrs->alt() ?>">

The user can easily set attributes like the alt text, classes etc. via the block settings. So far, so good.

But I’d like to reference the alt text image metadata from a sidecar (.txt) file, based on a file template. How would I go about referencing this in the image snippet?

I see that the image id and guid is present in the block attribute data, can I go from there?

You can use the image() helper to fetch an image with its id: https://getkirby.com/docs/reference/templates/helpers/image

Thanks!

<?= image($attrs->id()) ?>

doesn’t work, because the id contains the page name (mypage/xyz.jpg). But if I pass just the filename (‘xyz.jpg’) a proper file object is returned. Is there a way to get just the file name without the page part?

Hm, I thought the image helper accepted the id, at least it does in the example in the docs, but maybe that doesn’t work if you use it on the current page. Shame. I’ll have to look what other options we have.

I already had the solution in one of my templates:

$image = $attrs->id()->toFile();
1 Like

Thanks,

$alt = $attrs->id()->toFile()->content()->alt();

is what I was looking for and it works! :slightly_smiling_face:

Great support once again.

1 Like