Counts of nested MIGX TVs

Hey all, I’m hoping someone can help me here. I am using MIGX TVs to add content sections to a webpage. Where more than one image is added I am populating a carousel, and where there is a single image I am just displaying that image. I am wanting to use the Modx IF plugin to output different chunks but I am having some difficulty figuring out how to just get the image count.

My TV setup:

  • Section (migx)
    • Title
    • Images (migx)
      • Image
    • o content

My If call:

[[!If? 
&subject=`[[+images]]` 
&operator=`GT` 
&operand=`1` 
&then=`<div class="owl-carousel">[[getImageList? &value=`[[+images]]` &tvname=`image` &tpl=`carouselImage`]]</div>` 
&else=`[[getImageList? &value=`[[+images]]` &tvname=`image` &tpl=`sectionImage`]]`
]]

[[+Images]] outputs is array rather than an integer, so it isn’t working in the line subject='[[+images]]' . Is there a quick way to get the image count from the nested Images TV to use here? I have tried using [[+total]] instead, but it gives me an unexpected value.

Doesn’t [[+images]] return a JSON string?

If that’s the case, you could use a snippet to get the count (untested):

[[!GetCount? &data=`[[+images]]` ]]
/* getCount snippet */

$data = $modx->getOption('data', $scriptProperties);
$decodedArray = $MODX->fromJSON($data);
    return $count($decodedArray);

There seems to exist a property &tpl_oneresult in the snippet code:

So maybe this works:

[[getImageList?
	&value=`[[+images]]`
	&tpl=`carouselImage`
	&tpl_oneresult=`sectionImage`
]]

Hi @halftrainedharry,

Good spotting! However to make that work there would need to be row template for single result too, which unfortunately it doesn’t look like there is.

Otherwise your suggestion does work.

Hi @bobray, thanks for your reply.

You are correct, [[+images]] does return a JSON string. Your suggestion didn’t work as it was, but after a bit of tinkering I managed to get there. Here is my result:

[[GetImageCount? &data=`[[+images]]`]]
<?php
/* GetImageCount snippet */

$images = json_decode($data);
$numberOfImages = count($images);
return $numberOfImages;
[[!If? 
&subject=`[[GetImageCount? &data=`[[+images]]`]]` 
&operator=`GT` 
&operand=`1` 
&then=`<div class="owl-carousel">[[getImageList? &value=`[[+images]]` &tvname=`image` &tpl=`carouselImage`]]</div>` 
&else=`[[getImageList? &value=`[[+images]]` &tvname=`image` &tpl=`sectionImage`]]`
]]

Thanks heaps for helping me get there!

you could work with tplFirst and tplLast to add your surrounding carousel div

With a construct like yours, this could result in parsing issues, when you have large results and both getImageList calls will allways be parsed here.

Also, there is no reason to call If uncached here.

If you go this route with the custom snippet, why not put all the logic into your snippet and run getImageList with $modx->runSnippet inside GetImageCount

Then you don’t need that If snippet construct.

Hi @bruno17,

thank you for your comments here. That is an interesting point about parsing issues and both calls being parsed.

Can you please give me an example of how to use tplFirst and tplLast?

Thanks!

[[getImageList?
	&value=`[[+images]]`
	&tpl=`carouselImage`
	&tpl_oneresult=`sectionImage`
        &tplFirst=`carouselFirst`
        &tplLast=`carouselLast`
]]

carouselFirst:

<div class="owl-carousel">
--same as your carouselImage--

carouselLast:

--same as your carouselImage--
</div>

There are also the placeholders [[+_first]] and [[+_last]] that you could use in the “carouselImage” chunk:

[[+_first:eq=`1`:then=`<div class="owl-carousel">`]]
<!--  previous chunk content -->
[[+_last:eq=`1`:then=`</div>`]]

Or an extended “GetImageCount” snippet would look something like this:

$images = json_decode($data);
$numberOfImages = count($images);

$props = ['value' => $data];
if ($numberOfImages == 1) {
    $props['tpl'] = 'sectionImage';
} else {
    $props['tpl'] = 'carouselImage';
    $props['wrapperTpl'] = '@CODE: <div class="owl-carousel">[[+output]]</div>';
}
return $modx->runSnippet('getImageList', $props);
1 Like

Thanks everyone for your help here, much appreciated!