Creating a dynamically updating list of items using a MIGX config list in MODX

299 Views Asked by At

Please bear with me as I try to explain this. Using https://dev.bipc.com/people/ as my example, I'm trying to dynamically update the list of items in the "admissions" drop-down to only show items that are currently used by our staff.

Presently, it's just a list generated using the MIGX Loop Collection snippet:

[[migxLoopCollection?
    &packageName=`myPackage`
    &classname=`myClassname`
    &sortConfig=`[{"sortby":"name"}]`
    &tpl=`@CODE: <option value="[[+uid]]">[[+name]]</option>`
    &toPlaceholder = `listPlaceholder`
]]

However, this method shows all items not just ones that have been selected by users and this results in items filtering all people out and showing nothing.

Instead, I was trying to create a list using only published staff resources that I could feed into an array, remove duplicates and output a formatted line item based on the &tpl code above.

I started off using PDO Resources to select all parent IDs of my resources, limit the scope to 5 users for testing, define my chunk template and Template Var names.

[[pdoResources?
    &parents=`xxx`
    &depth=`0`
    &limit=`5`
    &tpl=`list-courtTpl`
    &includeTVs=`LastName,Admissions`
    &sortby=`{"LastName":"ASC"}`
    &toPlaceholder=`listItems`
]]

[[+listItems:removeDupsList]]

list-courtTpl only included getImageList to collect all the items from each resource:

[[getImageList?
    &tvname=`Admissions`
    &tpl=`list-CourtNameTpl`
    &docid=`[[+id]]`
]]

list-CourtNameTpl plays with the unique ids and outputs their Names only with the getCourName snippet.

[[+getCourtsList:getCourtName:trimWhiteSpace]]

The trimWhiteSpace snippet feeds in each Admission item and adds a double pipe separator and trim whitespace surrounding each item (which it's not doing):

$output = $input . "||";
echo trim($output);

The removeDupsList snippet is supposed to take the current list and explode each item into an array, remove the duplicates using array_unique, sort the data and output each value:

$list_input = explode('||', $input);
$list = array_unique($list_input);
sort($list);

$output = '';
foreach ($list as $x => $value) :
    $output .= $modx->getChunk('list-CourtItemTpl',array(
      'courtItem' => $value
    ));
endforeach;
echo $output;

Now here's the problem, I keep getting a string of items with spaces in between like this:

Washington DC Florida Texas Washington DC Pennsylvania New Jersey

And you can see that the duplicate item for Washington DC has not been removed. I'm assuming because the first item has no space in front of it.

Any help would be greatly appreciated.

Thanks

0

There are 0 best solutions below