Linking to dynamic pages in MODX through TinyMCE's search

We've recently launched a site which has a large number (800+) of dynamic pages. It wasn't practical or necessary to create individual MODX resources for each page so the content is controlled using the awesome MIGXDB which made the creation of the database tables and the CMPs (custom manager pages) a breeze.

Linking to these pages caused a bit of a problem however and we didn't want to force our client to have to copy and paste URLs into TinyMCE so we got digging. When you use the search facility in TinyMCE MODX performs a simple search on the pagetitle or the alias. This script can easily be extended to include your dynamic pages, a list of downloads, or anything you'd want your users to link to.

This loops through the matching resources and outputs the title and the ID, then a pipe (|) then the ID. All we need to do is add in our own links after this loop and they'll appear in the search results.

A crude example would be to add this after the code above:

echo 'Studio Republic|http://www.studiorepublic.com'."\n";

The script that this is returned to is expecting an integer value which it wraps in the MODX resource link tag (eg [ [ [~99] ]) so we need to adjust this script:

This uses the function below to check if the returned value is an integer. If it is, it wraps it in the MODX link tag, if not is uses the value passed to it. Here's the isNumber function:

function isNumber (o) { return ! isNaN (o-0) && o != null;}

With all this in place, you'll now be able to select the link to our site will appear below your standard MODX resource search results.

We added our own search function and appended the results to the list, but you could easily add both sets or results to an array, merge the two and output a single list of results. One stumbling block we came up against was that the list of links is limited to 10 results. Another hack was necessary, this time to the JQuery autocomplete function here:

As with all these sort of hacks, you'll need to be careful when upgrading MODX as these files could be overwritten. Always keep a local copy and make a note of what you've changed so you can restore them if necessary.