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.

The script in question is located here:

assets/components/tinymce/jscripts/tiny_mce/plugins/modxlink/search.php

You'll see the bit we're interested in which is:

foreach ($resources as $resource) {
   echo $resource->get('pagetitle').' ('.$resource->get('id').')|'.$resource->get('id')."\n";
}

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:

assets/components/tinymce/jscripts/tiny_mce/plugins/modxlink/js/modxlink.js

At line 32 you'll see this:

$('#href').val('');

Which I changed to this:

if(isNumber(item[1])){
$('#href').val('');
} else {
$('#href').val(item[1]);
}

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:

assets/components/tinymce/jscripts/autocomplete/jquery.autocomplete.js

At line 22 you'll see:

max: options && !options.scroll ? 10 : 150

Just increase the 10 to get more results in the list.

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.

WINCHESTER

LONDON

SEND US A MESSAGE

WINCHESTER

10 Charlecote Mews
Winchester
Hampshire, SO23 8SR

01962 659123
[email protected]

LONDON

86-90 Paul Street
London
EC2A 4NE

0203 507 1842
[email protected]