Read both files and texts from a structure

If you have the following structure in the Blueprint:

Persons:
    label: People
    type: structure
    fields:
        image:
             label: Image
             type: files
        alt:
             label: Description
             type: text
        link:
             label: URL
             type: text

Which ‘tostructure’-code is necessary in the template?
How can you read both files and texts from a structure like this?

Something like that with some HTML in the right places:

$items = $page->people()->toStructure();
foreach ($items as $item):
  $images = $item->image()->toFiles(); // or `toFile()` and no foreach if it is just one file
    foreach ($images as $image):
      echo $image;
    endforeach;
  echo $item->alt();
  echo $item->link();
endforeach;

Thank you!

I nevertheless did not manage to display a single image this way. After some trial and error, the following worked:

<?php $items = $page->persons()->toStructure(); ?> 
<?php foreach ($items as $item): ?> 
   <?php if($image = $item->picture()->toFile()): // i use 'picture' now, because 'image' is misleading ?>
            <img src="<?= $image->url() ?>" alt="<?php echo $item->alt(); ?>"/>
   <?php endif ?>
<?php endforeach; ?>

Ha, ok, your fieldname was persons not people… I read the label instead of the fieldname.

Note that if you select more than one image in your picture field, you need the loop. If you don’t want that, better restrict the maximum selectable images.

hello, i followed your rule but the image is not showing up, no error message, other text displayed, any help?

Could you provide details of your problem or your code?

<?php $items = $page->testi()->toStructure(); ?>
<?php foreach ($items as $item): ?>
    <?php if ($image = $item->image()->toFiles()): ?>
        <img src="<?  $image->url() ?>"  />
    <?php endif ?>
    <?= $item->TestimonyName() ?>
    <?= $item->TestimonyRank() ?>
    <?= $item->TestimonyText() ?>
 <?php endforeach ?>

This code doesn’t make sense.

Either you have a single file, then you need to convert the field value with toFile() instead of toFiles():

<?php if ($image = $item->image()->toFile()): ?>
  <img src="<?= $image->url() ?>"  />
<?php endif ?>

Or you have multiple files (in this case, you don’t need an if statement, but you need to loop through the result).

<?php
$images = $item->image()->toFiles();
foreach ( $images as $image ): ?>
    <img src="<?= $image->url() ?>"  />
<?php endforeach ?>

On a side note, your image tag is missing an alt attribute. This attribute is always required for accessibility purposes, even if left empty (in case of solely decorative images).

it’s just a single image, i did the correction but not showing still.

Ah, I overlook the missing echo

<img src="<?= $image->url() ?>"  alt="" />

I put echo but same problem, no picture displayed

Could you please post your blueprint?

Testi:

sections:

fields:

  Testi:

    label: Testimonies

    type: structure

    sectionField: section

    fields:

      image:

        label: Image

        type: files

      TestimonyName:

        label: Name

        type: text

      TestimonyRank:

        label: Rank

        type: text

      TestimonyText:

        label: comment

        type: textarea

Hm, what about when you inspect the source code in dev tools. Is the image tag and url shown?

this is what i can see when i inspect

?

[quote=“texnixe, post:16, topic:14261, full:true”]
?
[/quote]

oops, the html disappears

Please use three backticks on a line before and after the code snippet to post code.

 <img src="<? echo $image->url() ?>" class="testimonial-img" alt="">