Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This MODX plugin hides certain Content Blocks buttons from a specific MODX user group. It also disables the drag and drop functionality, so that you can provide some users with a more locked down version of Content Blocks.
<?php
/**
* Content Blocks cbContentEditor Plugin
*
* This plugin hides certain CB buttons from certain MODX user groups
* and disables the drag and drop functionality, so that you can provide
* some clients with a more locked down version of CB.
*
* I would suggest setting up CB templates and defaults in the CB component manager,
* so that predefined layouts are loaded automatically when a new resource is created.
* It is also recommended that you hide the main content area on "resource/create" using form
* customisation, other wise when a user creates a new page, they will be able to delete blocks
* and use drag & drop, because no Resource ID has been defined yet (this only happens on save).
*
* Tested on: MODX 2.6.1, Content Blocks
*
* Events: OnDocFormPrerender
*
* @author Jon Leverrier <jon@youandmedigital.com>
*
*/
switch ($modx->event->name) {
// load on system event OnDocFormPrerender
case 'OnDocFormPrerender':
// get the current resource ID
$resource = $modx->resource;
// for newly created resources, there will be no ID until the page is saved. So if there
// is no ID, stop here...
if (!$resource) {
return;
}
// check to see if CB is enabled on a resource
$isContentBlocks = $resource->getProperty('_isContentBlocks', 'contentblocks', null);
// only load for a specific user group and if CB is enabled on the resource
if ($modx->user->isMember('Client Web Editor') && $isContentBlocks = true) {
// load custom script to prevent drag and drop feature in CB
// (need to find a better way todo this)
$modx->regClientStartupHTMLBlock('
<script>
jQuery(function($) {
var checkCB = setInterval(function() {
if (ContentBlocks.initialized) {
$("#contentblocks .contentblocks-field-wrap").addClass("prevent-drag");
clearInterval(checkCB);
}
}, 50);
});
</script>
');
// remove certain buttons from the CB interface
$modx->controller->addHTML('
<style>
.contentblocks-field-delete,
.contentblocks-add-block,
.contentblocks-add-layout,
.contentblocks-layout-menu,
.contentblocks-layout-move-up,
.contentblocks-layout-move-down,
.contentblocks-add-content-here,
.contentblocks-layout-settings,
.contentblocks-field-gallery-url,
.contentblocks-field-upload,
.contentblocks-field-image-url {
display: none !important;
}
</style>
');
} else{
// if the check fails, do nothing
return;
}
break;
}
@labr1005

This comment has been minimized.

Copy link

labr1005 commented Nov 13, 2017

  1. I would change the event to "OnDocFormPrerender". ContentBlocks is only available on Resource pages. "OnManagerPageBeforeRender" fires on every manager page.

  2. You initialize a timeout, so your script will execute after 500 milliseconds no matter if ContentBlocks is ready or not.
    This script loops and checks every 100 millisecond until ContentBlocks.initialized returns true and then executes your command:

<script>
    window.onload = function() {
        var checkCB = setInterval(function() {
            if (ContentBlocks.initialized) {
                $("#contentblocks .contentblocks-field-wrap").addClass("prevent-drag");
                clearInterval(checkCB);
            }
        }, 100);
    }
</script>

Untested. :)

@jonleverrier

This comment has been minimized.

Copy link
Owner Author

jonleverrier commented Nov 20, 2017

thanks @labr1005 - I have incorporated your changes. +100 for checking if CB was initialised :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.