-
-
Save jonleverrier/5964596e3bbc8f15a4f305185762eae6 to your computer and use it in GitHub Desktop.
<?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 <[email protected]> | |
* | |
*/ | |
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; | |
} |
thanks @labr1005 - I have incorporated your changes. +100 for checking if CB was initialised :)
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
Hi @inreti-sb - I've not used MODX for a while, so might not be much help.
I would check:
- Does line 41 get added to the Dom? If yes;
- Does the HTML element
#contentblocks .contentblocks-field-wrap
exist in the latest version of CB? If yes; - 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; - 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!
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:
- Option to disable the drag&drop image upload
- 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?
I would change the event to "OnDocFormPrerender". ContentBlocks is only available on Resource pages. "OnManagerPageBeforeRender" fires on every manager page.
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:
Untested. :)