Retrieve file blueprint field on template

Hello! I’ve defined a featured image on my page blueprint, and have been trying how to retrieve information from its fields (defined on the image blueprint) with no luck.

Let’s go by parts. Here’s my code on the post blueprint for featured image:

        label: Featured image
        type: files
        layout: cards
        max: 1
          template: image
          cover: true

And then, inside blueprints/files/image.yml I have the following (fragment):

        type: fields
            label: Alternative Text
            type: text
            label: Photogapher
            type: text
            width: 2/3

Now, if there’s info on the Photographer field, how can I retrieve it and display on my template? Any ideas?

How do you get the image? It should be something like this

if ($image = $page->featured()->toFile()) {
  echo $image->photographer();

So, just like for the URL, you need your image/file object first.

Thanks! It’s working now. I was trying some other approaches that failed. My final code it’s like this:

  <!-- Featured image -->
  <?php if($image = $page->featured()->toFile()): ?>
    <div class="featured">
      <div class="image" style="background-image:url(<?= $image->url() ?>);" ></div>
      <span class="caption">
        <?= $image->photographer(); ?>
  <?php endif ?>

What’s probably missing is to check if the photographer field has some content or not, right?

Yes, if the field is not required, you can check if it is empty or not:

<?php if ($image->photographer()->isNotEmpty() : ?>
<span class="caption">
        <?= $image->photographer(); ?>
<?php endif ?>

On a side note: consider using a figure tag with an image and a figcaption instead of divs with background images to make your image more semantically useful and accessible.

Thanks again! Also for your suggestions. I’ve already changed the code to reflect them :slight_smile: