MODX Quick Tip: Resource List TVs

Jul 16, 2012

MODX Quick Tip: Resource List TVs

The Problem

Let's say you need a MODX Template Variable to return a list of Resources from which a Manager user can select, to control the display of content from different areas of the site, for example. MODX has a built-in TV input type called Resource List that does just that - lists resources and returns the ID of the selected one.

The problem is that ALL the Resources show up unless you filter the results, and the only way to do that is with Where Conditions. Until recently, the documentation didn't provide any examples. I fixed that, but I thought I'd re-iterate it here:

[{"template:=":"4"}]

Break it down:

  • Make sure you use the outer square brackets. That took me forever to figure out. It sometimes works without, but it seems much more reliable if an array with a nested object is entered.
  • Inner curly braces = also required but more obviously so :) Similar to the way you can define query conditions in JSON format with getResources.
  • The above lists all resources whose template has an ID equal to 4.

Here's another example:

    [{"parent:IN":[34,56]}]

That gets you all Resources whose parent ID is in the array. Note: I've deduced that MODX is using $modx->fromJSON() and passing the resulting array to a xPDO query. So if you think about what are valid values in that context, quotes around integers are ok, but arrays can't be wrapped with quotes, or they are interpreted as strings. Also "34,56" doesn't work because you can't use :IN with comma-separated lists of integers.

Yet another example:

    [{"pagetitle:!=":"Home"}]

That gives you all the Resources that do NOT have the pagetitle: Home.

It's that easy. Now go make something :P