Block error: "Array to string conversion"

Hi awesome people of the forum,
probably a newbie question, but I am getting an “array to string conversion” error in a custom block and I can’t figure out why.
The problem is this line of code.

<img src="<?= $block->teaser_image()->url() ?>">

I also tried

<?= $block->teaser_image() ?>
<img src="<?= $block->image()->url() ?>">
<?= $block->image() ?>

This is the corresponding part of my blueprint

teaser_image:
    type: files
    label: Beitragsbild
    layout: cards
    accept: 
      extension: jpg, png, jpeg
      maxsize: 65000
    multiple: false

What’s wrong here?

You always have to convert a value from a files field into a file object first. This is done with the toFile() method for a single file:

<?php if($image = $block->teaser_image()->toFile()): ?>
  <img src="<?= $image->url() ?>" alt="<?= $image->alt() ?>">
<?php endif; ?>

Note that the if statement is necessary to prevent errors in case the file does not exist.

2 Likes

Ohh, I see. Thank you so much!