ContentBuilder Tip: Variable Stuffing

Here's how to get past a minor oversight in ContentBuilder. You can dynamically add JavaScript and such to the editor template while it's being rendered, such as to put a WYSIWYG editor on the form.

ContentBuilder is a power-user tool, so let's start by abusing our power.For all ContentBuilder's power, it has severe limitations. While imagining to what end and extent I might use it, I started imagining solutions to its inherent problems, the greatest of which I found was actually solvable, in a limited fashion.

The problem is that ContentBuilder enables easy form creation for data inputs it acknowledges, and customization in ways it chooses, but stops there. If default textareas and one flavor of date picker suffice, you're quickly done.

If you need to force-feed one of your textareas to CodeMirror, however, ContentBuilder gets in your way. It has been put in control of your data-handling, and it will fight you for its domain.

I found the solution was to congratulate its diligence and then stealth-in the extra behavior. For example, to make CodeMirror take possession of part of the form, I just needed to add properly-escaped invocation code to a subsequent form element. When processing hits the Editable Prepare stage, you can directly edit the snippets of HTML it's about to splice together for your working form.

So, if you have a source field and a language field just below it, you can prepend the language field's SELECT box with the invocation code to modify the code TEXTAREA.

Some types of input will be easier to modify in this way, than others. If you're not sure how to modify your editable template so it allows these shenanigans, you can add a useless variable to the table (yay, database bloat! -- but it works) and represent it in your editable template with a INPUT type="hidden".

I'm not saying these are especially well-recommended practices overall, but they're suitable advice when considering ContentBuilder, which, as I'll repeatedly say, is excellent, but is only to be trusted in the most cautious and controlled manner. It's almost less work to write actual new extensions, but "almost" is a sketchy word when both time and money are involved, or when a deadline's many little things are pecking you to death.

When forcing ContentBuilder to modify an input in this way, be sure to test the reliability of the form's ability to post and save data afterwards. Test in multiple browsers to be sure - ContentBuilder may have been relying on certain assumptions your third-party widget didn't obey. That said, for most input types, i.e. anything expecting arbitrary alphanumeric content, obeying ContentBuilder's weird semi-random field-naming conventions will suffice.

Even this solution has limitations; you can quickly find yourself ending and restarting textareas and losing track of conditional logic threads. If you ever found yourself needing to depend on this sort of behavior too much, as your "temporary prototype" begins to shamble its way out of Frankenstein's control, that's a good time to transition to making a proper Joomla extension of your own -- and you can probably create partial forms in AJAX which you can "temporarily" embed into ContentBuilder's forms, as you go.