ContentBlocks Restricted by Template

The Problem

If you haven’t had a chance to try out the ContentBlocks Premium Extra for MODX, I highly recommend you do. For twenty to thirty percent of the sites I work on nowadays, ContentBlocks is an indispensable tool to give site owners and content creators the power to do almost anything imaginable with structured layouts.

That said, it seems on nearly every install, a custom plugin is required, to scope the action of the ContentBlocks plugin to specific Templates. Not every Resource, or content type, will benefit from ContentBlocks, and often, once the Resource edit view loads, it’s too late to preserve the contents of the “content” field. It’s also an extra step for site admins to have to disable ContentBlocks for Resources that won’t be using it.

The Solution

This small plugin will let you specify a comma-separated list of Template IDs for which to enable ContentBlocks, while Resources using other Templates will be ContentBlocks-disabled by default.

Note that you have to create the system setting with the key contentblocks.enabled_template_ids and supply the Template IDs, otherwise ContentBlocks will never be enabled. Also, you will want to enable this custom plugin for the ‘OnDocFormPrerender’ event, and give it a “Priority” of 0

The default ContentBlocks plugin should then be set to fire on that event with “Priority” of 1.

That’s it. Hopefully this helps you fully utilize the ContentBlocks Extra to your specific needs. Vive la MODX!