Get image from structure with ToggleField plugin


#1

Hi,
I can’t get my image from a structure field. I’m using the fieldtoggle plugin. This is my blueprint

  gallery:
    label: Galerie
    type: structure
    entry: >
      {{imagetoggle}}, {{legende}}
    fields:
      imagetoggle:
         label:       Image ou vidéo
         type:        fieldtoggle
         options:
            image:       "Image"
            video:        "Vidéo"
         show:
            image:       imagefield
            video:        video
         hide:
            image:       video
            video:        imagefield
      imagefield:
         label:       Image
         type:        image
      video:
         label:       Vidéo
         type:        text
         icon: link
         help: 'ID de la vidéo à intégrer. Pour youtube'
      legende:
        label: Légende
        type: textarea

And I’m just trying to echo them like that

<?php $gallery = $pageworks->gallery()->yaml(); foreach($gallery as $gal): ?>
     <?php $img = $gal['imagefield'] ?>
     <?php $caption = $gal['legende'] ?>

     <figure>
        <?php echo $img ?>
        <img class="lozad" data-src="<?php echo thumb($img, array('width' => 1350, 'quality' => 100))->url() ?>" alt="...">
        <figcaption><?php echo $caption ?></figcaption>
     </figure>

     <?php endforeach ?>

The $gal['imagefield'] is not writing the correct URL.
Even if I try $img = $gal['imagefield']->toFile(), the data-src is always empty…


#2

toFile() is a field method, and as such, you can only use it with a field object.

$gal['imagefield'], however, is just a string that contains the filename. You therefore have to get. the file object from the page like this:

if($image = $page->image($gal['imagefield'])) {
  echo $image->url();
}

#3

Didn’t think things like that. Thanks a lot for your explanation !!!


#4

As an alternative, you could use the toStructure() field method instead of yaml. If you then call a structure field using arrow syntax ($gal->imagefield()), you get a field, and you can then use toFile()again.

<?php 
$gallery = $pageworks->gallery()->toStructure(); 
foreach($gallery as $gal): ?>
     <?php if ($img = $gal->imagefield()->toFile()):  ?>

     <figure>
        <img class="lozad" data-src="<?php echo thumb($img, array('width' => 1350, 'quality' => 100))->url() ?>" alt="...">
        <figcaption><?php echo $caption ?></figcaption>
     </figure>

   <?php endif ?>
<?php endforeach ?>