Skip to content

Instantly share code, notes, and snippets.

@sepiariver
Last active December 21, 2017 12:09
Show Gist options
  • Save sepiariver/5ba8cc2a74e3a14d3960 to your computer and use it in GitHub Desktop.
Save sepiariver/5ba8cc2a74e3a14d3960 to your computer and use it in GitHub Desktop.
Scope ContentBlocks (modmore's premium plugin for MODX) to specific templates
<?php
$eventName = $modx->event->name;
switch($eventName) {
case 'OnDocFormPrerender':
if (!is_object($resource)) { // prevents bad error when user doesn't have perms to resource
$modx->log(modX::LOG_LEVEL_ERROR, '[ContentBlocks Templates] No Resource Object on line: ' . __LINE__);
return;
}
// a system setting must be created with the key 'contentblocks.enabled_template_ids'
$enabledTemplates = array_map('trim', explode(',', $modx->getOption('contentblocks.enabled_template_ids')));
$forced = $modx->getOption('force_disable', $scriptProperties, 0);
if ($forced && !(in_array($resource->get('template'), $enabledTemplates))) {
$resource->setProperty('_isContentBlocks', false, 'contentblocks');
$resource->save();
break;
}
$isContentBlocks = $resource->getProperty('_isContentBlocks', 'contentblocks', null);
if ($isContentBlocks !== true) {
$disabled = true;
if (in_array($resource->get('template'), $enabledTemplates)) $disabled = false;
if ($disabled) {
$resource->setProperty('_isContentBlocks', false, 'contentblocks');
$resource->save();
}
}
break;
default:
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment