Show a Drop-Down List for the Template Field in MODX NewsPublisher

In the last article, we discussed how to show a drop-down list of parent names in NewsPublisher using a template. You might have wondered if you can do the same thing for the Resource’s Template. You can, but the technique is slightly different. Obviously, you can’t use a Resource List TV, since Templates are not Resources, but with a slight modification to the TV, it will work fine.

Much of this article duplicates the content of the previous one, since not everyone will have read that one.

The Method

As with showing the parent list, the secret is to leave out the template field from the &show property and provide a TV called “Template” that takes its place. Thanks again to MODX user David Pede for suggesting the idea of using a TV.

Creating The TV

On the Elements Tree, Right-click on “Template Variables” and select “New Template Variable”

On the “General” tab, put Template for both the name and the caption. Be sure to capitalize the first letter. If you enter a description, whatever you enter will be shown as hover help when the mouse is over the list. I find that annoying, but you may not. To avoid it, leave the description blank.

Select “Listbox (single-select)” for the Input Type. On the “Input Options” tab, list the Templates you want to show using the standard MODX delimiters. Each Template will take the form TemplateName==TemplateID, and the entries will be separated by ||, like this:


The number after the == is the ID of the Template (shown in parentheses after the Template name in the Elements tree).

If you want to show all Templates, or all Templates in a category, you may be able to use an @SELECT binding in the Input Option Values field, like one of the following:

@SELECT templatename,id FROM modx_site_templates
@SELECT templatename,id FROM modx_site_templates WHERE category=0

There’s a catch, however. If E_NOTICE warnings are turned on (they shouldn’t be on a production site), everything will work, but you’ll see a raft of E_NOTICE warnings above your NewsPublisher form. This happens inside the MODX modTemplateVar class. I wish I knew why.

If you set the Allow Blank field to Yes, you’ll see a dash at the top of the drop-down list. The user may select this, and unless you’ve included “Template” in the &required property of the NewsPublisher tag, the Resource may be saved with no Template.

Actually, you may see that dash regardless of the Allow Blank setting, due to a bug in NewsPublisher that will be fixed in the next version.

If you set “Allow Blank” to No, there is another problem that may arise. In the Manager, when you try to save any Resource that uses a Template which has the Template TV attached without setting that TV, you’ll get a cryptic popup error message (that doesn’t mention the TV), and you won’t be able to save the Resource until you select a value for the TV. The way to avoid this is to set a default value for the TV. Set it to the ID of any existing template. Note that the default you select will be pre-selected when NewsPublisher displays the drop-down list.

You may choose to set “Allow Blank” to Yes and live with the dash. If you include the Template TV in the &required property in the NewsPublisher tag, NewsPublisher will prevent saving the resource without selecting a Template.

Displaying the TV

To show the TV, just include its name in the &show property in the NewsPublisher snippet tag. By default, the list of templates will be shown as a scrollable list showing 8 templates at a time. To make it look like a standard drop-down list, put this property in the NewsPublisher snippet tag:


If you like the scrollable list, you can adjust the &listboxmax property to make it longer or shorter.

Creating the Plugin

If you are already using the NewsPublisherParent plugin, simply insert the following two lines just above the save() line at the end and skip the code below.

$resourceTemplate = $resource->getTVValue('Template');
$resource->set('template', $resourceTemplate);

The plugin code itself is very simple. On the Elements tab, right-click on “Plugins” and select “New Plugin”. Call the plugin “NewsPublisherTemplate” and paste this code into the “Plugin Code” section:

/* keep this from running in the Manager */
if ($modx->context->get('key') == 'mgr') {

/* (optional) Don't execute for existing resources --
   Remove the following line to let users modify
   existing templates in NewsPublisher */

if (!($mode == modSystemEvent::MODE_NEW)) return;

/* get Template ID from TV */
$resourceTemplate = $resource->getTVValue('Template');

/* save the Template id in the resource's template field */
$resource->set('template', $resourceTemplate);

On the “System Events” tab, put a checkmark next to OnDocFormSave and click on the “Save” button at the upper right.

Wrapping Up

Once the Template TV is created, you’ve modified the NewsPublisher Tag to include it, and you’ve created the plugin, you should be good to go. Your users should be very grateful.

For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under cPanel/Linux Hosting, DotNet/Windows Hosting, MODX, MODX | RSS 2.0

Author Spotlight

David Gurba

David Gurba

I am a web programmer currently employed at UCSB. I have been developing web applications professionally for 8+ years now. For the last 5 years I’ve been actively developing websites primarily in PHP using Drupal. I have experience using LAMP and developing data driven websites for clients in aviation, higher education and e-commerce. If you’d like to contact me I can be reached at

Leave a Reply

Your email address will not be published. Required fields are marked *

9 − 4 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>