October 9, 2019, 3:09pm
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:
October 9, 2019, 3:38pm
<?= 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();
October 9, 2019, 6:35pm
$alt = $attrs->id()->toFile()->content()->alt();
is what I was looking for and it works!
Great support once again.
Can you explain what’s going on in this line? It’s not very clear to me.
Also, I do not understand why the plugin to not send a proper image object directly to the template.
EDIT: In fact you have access to the $image object. I asked for a change.