List resources from multiple parents

Hi all!

I found a really useful snippet to list resources from a particular parent in a Listbox *(multiselect).
I\m using this to select ‘related products’.

@EVAL return $modx->runSnippet('listMyResources',array('parent' => 82));

I want to be able to select from multiple parents though - and if I try this:

@EVAL return $modx->runSnippet('listMyResources',array('parent' => 82,89));

it doesn’t work.

I think the ‘listMyResources’ snippet needs editing, but I don’t know what to do - this is the snippet:

<?php
$parent = $modx->getOption('parent',$scriptProperties,9);
$parentObj = $modx->getObject('modResource',$parent);
if (!($parentObj instanceof modResource)) { return ''; }
$resArray = $parentObj->getMany('Children');
$resources = array();
foreach($resArray as $res) {
  if ($res instanceof modResource) {
    $resources[] = $res->get('pagetitle') . '==' . $res->get('id');
  }
}
$out = implode("||",$resources);
return $out;

I’m sure it’s a simple fix, but I can’t work it out.

Any ideas?

Thanks
Andy

You have to change the call to this: (put 82,89 into an array)

@EVAL return $modx->runSnippet('listMyResources',array('parent' => [82,89]));

and change the beginning of the snippet to this:

$parent = $modx->getOption('parent',$scriptProperties, [9]);
// $parentObj = $modx->getObject('modResource',$parent);
// if (!($parentObj instanceof modResource)) { return ''; }
// $resArray = $parentObj->getMany('Children');
$resArray = $modx->getCollection('modResource', ['parent:IN' => $parent]); // NEW CODE!
$resources = array();
...

BTW: @EVAL won’t work anymore in MODX 3!

Yep - like always you have cracked it!
Every time I post I learn a bit more so thank you.!

yes I am aware it won’t work on MODX 3 - I can’t upgrade the site yet, but I believe it would change to something like this If I’ve read the docs correctly;

@SNIPPET listMyResources {"parent":"[82,89]"}

Although, not sure if the new array will work out of the box?

Don’t put the array in double quotes ("):

@SNIPPET listMyResources {"parent": [82,89]}

Ah yes OK that’s great thanks.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.