Skip to content

Instantly share code, notes, and snippets.

@jonleverrier
Last active December 30, 2021 22:49
Show Gist options
  • Save jonleverrier/5964596e3bbc8f15a4f305185762eae6 to your computer and use it in GitHub Desktop.
Save jonleverrier/5964596e3bbc8f15a4f305185762eae6 to your computer and use it in GitHub Desktop.
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
Copy link

  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
Copy link
Author

jonleverrier commented Nov 20, 2017

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

@inreti-sb
Copy link

Thank you for the plugin!
Works fine, but I can't prevent the drag and drop feature.

Apropo.. „// load custom script to prevent drag and drop feature in CB“ .. do I need it and where can I find it?
Thanks in advance!
Sebastian

@jonleverrier
Copy link
Author

jonleverrier commented Dec 20, 2021

Hi @inreti-sb - I've not used MODX for a while, so might not be much help.

I would check:

  1. Does line 41 get added to the Dom? If yes;
  2. Does the HTML element #contentblocks .contentblocks-field-wrap exist in the latest version of CB? If yes;
  3. If you add the class .prevent-drag to #contentblocks .contentblocks-field-wrap via your browser console does it prevent drag and drop? If yes, adjust the timeout value on line 48. If no;
  4. Might be worth checking in with Mark from Modmore to see if .prevent-drag was removed and if there is a new way to prevent drag and drop - the plugin worked at the time I wrote it!

@inreti-sb
Copy link

Hi @jonleverrier - thanks a lot for your fast reply and sorry for not answering earlier.

I just realized, that the plugin works as it should, but can't - up to now - fulfill one respectively two of my last following open wishes:

  1. Option to disable the drag&drop image upload
  2. Option to hide the text „or drop images here“ (unfortunately this text is not wrapped with a tag)

I will also ask for the option in this post...
https://forum.modmore.com/t/option-to-disable-the-drag-drop-image-upload-and-upload-from-url/699
... , but maybe someone has already extended this nice plugin or has a useful hint?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment