Can't access template fields for images saved in structure

Hi, thanks in advance for any assistance - I am new to the community and still learning.

I have a .yml file using the following structure:

        label: Carousel items
        type: structure
            label: Item title
            type: text
            label: Item subtitle
            type: text
            label: Item URL
            type: url
            label: Item Image
            type: files
            multiple: false
              template: image

I can use the following PHP to access the itemImage field (and other fields).

$cells = $part->cells()->toStructure();
foreach ($cells as $cell) : 
$image = $cell->itemImage()->toFile();

The problem is if I try to access any fields defined in the image template. I can only access fields from the default file template, but no fields from the custom image template. E.g. echo $image->caption()->html() yields no data even though I can see data in the caption field (defined in the image template) in the panel. But echo Html::img($image->url()) works just fine to display the image.

Note I do not have any issue with accessing custom fields from the image template if the itemImage file field is not part of a structure.

Is this an issue with the structure field type or am I doing something else wrong?

Thanks, Morgan.

Your code is alright, although make sure to check for an file image and don’t rely on $image to always be a file object.

What do you get if you print $image->template()->name()?

Hi, thanks for the response. I get the following error if I try that:

Error thrown with message "Call to a member function name() on string"
#15 Error in C:\xampp\htdocs\kirby\site\snippets\section-image-carousel.php:54

Line 54 is just: <?php print $image->template()->name(); ?>

Then try just $image->template()

Thanks, this returns “image” which is the correct template name.

For all images in the loop?

Correct, for each image in the loop.

Could you post the complete code where you output the caption and other meta data?

Here you go:

<div class="carousel mx-auto<?= ($part->prevNextButtons()->toBool() ? ' has-buttons' : '') ?>" data-flickity='<?= json_encode($options); ?>'>
        $cells = $part->cells()->toStructure();
        foreach ($cells as $cell) : 
          $hasUrl = $cell->itemUrl()->isNotEmpty();
          $image = $cell->itemImage()->toFile();
          $attributes = [ 
            'class' => 'img-fluid d-block mx-auto img-stretch',
            'alt' => $image->alt()->text() // outputs nothing although there is alt text in this field in the admin panel
          ]; ?>
          <div class="carousel-cell align-items-center">
            <div class="carousel-content">
              <?php if ($hasUrl == true) : ?>
                <a href="<?= $cell->itemUrl()->url() // outputs ok ?>" rel="noreferrer noopener">
              <?php endif; ?>
                    <?= Html::img($image->resize(327)->url(), $attributes) // outputs image ok ?>
                      <?= $image->caption()->text(); // outputs nothing although there is caption text in this field in the admin panel ?>
              <?php if ($hasUrl == true) : ?>
              <?php endif; ?>
          <?php print $image->template(); // outputs 'image' as template for each image ?>          
        <?php endforeach; ?>

I have also included the ‘image’ file template below:

title: Image
  mime: image/jpeg, image/png
    label: Caption
    type: text
    label: Alternative text
    type: text

All parts of the structure output as expected apart from the itemImage fields defined in the ‘image.yml’ template which I cannot seem to output.

Problem solved I think. I went in to check the images in this structure again (I was using the same image for each for testing) and there was an unsaved change in the admin panel. So those fields were simply empty until I saved the content which explains why it wasn’t working. I have just tested again and the fields are now displaying as expected. Sorry for the silly mistake and thanks again for trying to help.