How can ContentBlocks be ENABLED on specific templates only?

In ContentBlocks v1.10 and up, you can configure ContentBlocks to be enabled or disabled on specific templates by adding a template property.

Start by disabling ContentBlocks globally by changing the contentblocks.disabled system setting to 1. Alternatively, you can also create a context setting with that same key and value to only have it affect a specific context. 

Next you can add a property to the templates you want to enable. That property has the same key as the settings ( contentblocks.disabled) and should be set to 0 on the template(s) you wish to enable ContentBlocks for. The template property has priority over system and context settings. 

Detailed instructions for the template property can be found in the documentation.

Older versions (v1.9 and earlier)

In v1.9 and earlier it's not natively possible to manage ContentBlocks per template. To do so,reate a new Plugin in your MODX installation with the following contents:

<?php 
$eventName = $modx->event->name;
switch($eventName) {
     case 'OnDocFormPrerender':
         $disabled = true;
         $enabledTemplates = [1, 3, 5];
         if ($modx->controller && isset($modx->controller->resource) && $modx->controller->resource instanceof modResource) {
             $resource = $modx->controller->resource;
         }
         if ($resource && in_array($resource->get('template'), $enabledTemplates)) {
             $disabled = false;
         }
         if ($resource && $disabled) {
             $resource->setProperty('_isContentBlocks', false, 'contentblocks');
         }
         break; 
}

On the System Events tab, enable OnDocFormPrerender.

In this example plugin, ContentBlocks is only enabled whenever template 1, 3 or 5 is used - for other templates it is turned off. Please note that this will bypass the "Use ContentBlocks" setting on each of the resources.

Note: this only works if the ContentBlocks plugin has a higher priority number on the OnDocFormPrerender event than your own plugin. You can quickly edit this by right clicking the system event, choosing Update Plugin Event, and double clicking in the Priority column.

Still need help? Send us an email Send us an email