Check if structure field contains more than one item

Seems like a daft question but I cant seem to find the answer to this anywhere. Im using the images plugin which feeds a javascript slide show. if there is only one entry in the images field (which is technically a structure field), I want to just display that image on its own without the markup for the slideshow since there is no point.

How do i check if a structure has only one entry or more than one entry?

The yaml() method gives you an array. So you can use standard php methods to deal with an array:

<?= count($page->gallery()->yaml()) ?>

echos the number of elements in an array.

Thanks… I Probably should have mentioned i’m using toStucture()

surely theres a shorter way… this works but looks hideous

  <?php
  if($page->productimages()->toStructure()->count() > 1  )
  {
    $imageclass = "shop-product-media-set";
  } else {
    $imageclass = "shop-product-media-single";
  }
  ?>

Yes, you should have mentioned that…:wink:

If you just want a classname, you can use the ternary. operator

$imageclass = $page->productimages()->toStructure()->count() > 1? 'shop-product-media-set':'shop-product-media-single';

If you need different html output, you better stick with if-else.

Thats better :slight_smile:

Don’t need new markup, my base html works a treat with Tiny Slider. I just need to flip a class.

I cleaned it up a little further…

<?php
$imageset = $page->productimages()->toStructure();
$imageclass = $imageset->count() > 1? 'shop-product-media-set':'shop-product-media-single';
?>
<div id="product-slides" class="<?= $imageclass ?>">
  <?php foreach($imageset as $image): ?>
    <?php if($image = $page->image($image)): ?>
    <figure class="product-thumb">
      <?= $image->focusCrop(800,600)->html(); ?>
    </figure>
    <?php endif ?>
  <?php endforeach; ?>
</div>