MODX Quick Tip: Easy Multi-Select Resource List TV

Nov 2, 2012

Ze Problemo

MODX (practically, and maybe actually) invented Custom Fields in the CMS world - they're called Template Variables (TVs). There's many input types you can choose for a TV, so that it "grabs" a set of data for the end-user to choose from. The Multi-Select input type allows a user to select multiple items from a double-pipe delimited list that you specify in the input options. Resource List input type allows a user to select a Resource from the Resource Tree. BUT, what if you want the user to be able to select MULTI Resources? Out of the box, you can't.

Ze Answer My Friend, Is Blowin in the Manual

The Official Documentation on TV Input Types has had the answer all along!

It's in the Check Box section under Advanced Usage: https://docs.modx.com/revolution/2.x/making-sites-with-modx/customizing-content/template-variables/template-variable-input-types#TemplateVariableInputTypes-AdvancedUsage

Simply put the following @SELECT statement in the Input Option Values field, under the Input Options tab, when editing/creating the TV (this works with a Multi-Select dropdown as well as Check Boxes)

@SELECT pagetitle, id FROM modx_site_content WHERE parent=35

Break It Down:

This will query the database "FROM" the "modx_site_content" table, and return the "pagetitle" as the Name and "id" as the Value, "WHERE"ever the "parent=35". Replace "35" with whatever Resource is the container of the Resources you want to list.

THAT'S IT! I couldn't believe how simple it was. Because you're doing this in a Multi-Select dropdown or Check Box TV, the user can select more than one of the pagetitles returned, and the TV will output the ids. ****NOTE: You must select "Delimiter" as an Output Option, and specify comma "," as the delimiter, if you want to use the result as a property in a snippet call.**

We're done here :)

No, really. That's it. Now go make a website.

*UPDATE: Below are some additional usage examples, as well as a note for using getResources sorting options.

@SELECT pagetitle, id FROM modx_site_content WHERE parent IN (85,86,95,97,88) ORDER BY pagetitle ASC

This gets you all Resources that have ID 10,23,45, or 67 as their parent, sorted by pagetitle alphabetically.

@SELECT pagetitle, id FROM modx_site_content WHERE published=1 AND hidemenu=0

This gets you all Resources that are published and not hidden from menus.

To sort getResources by the order in which the Resources are entered into the Multi-select TV, call it something like this:

[[getResources? &resources=`[[*myMultiSelectResourceTV]]` &sortby=`FIELD(modResource.id,[[*myMultiSelectResourceTV]])` &sortdir=`ASC` ... ]]