Disallow Duplication of Elements #15122
-
I need to prevent authors from duplicating entries. Unfortunately, there's no specific permission on Users/Groups, but I do see How could I use that in an event or something? Specifically, I want to hide the 'Duplicate' action in the menu for all elements. I've also played with Any advice would be very helpful! |
Beta Was this translation helpful? Give feedback.
Answered by
brandonkelly
Jun 3, 2024
Replies: 1 comment
-
Create a module with the following code: use Craft;
use craft\elements\actions\Duplicate;
use craft\elements\Entry;
use craft\events\AuthorizationCheckEvent;
use craft\events\RegisterElementActionsEvent;
use craft\services\Elements;
use yii\base\Event;
// Remove the “Duplicate” bulk action on entry indexes
Event::on(Entry::class, Entry::EVENT_REGISTER_ACTIONS, function(RegisterElementActionsEvent $event) {
$event->actions = array_filter($event->actions, function($action) {
$type = is_array($action) ? $action['type'] : $action;
return $type !== Duplicate::class;
});
});
// Prevent the “Save as a new entry” action on entry edit pages
Craft::$app->elements->on(Elements::EVENT_AUTHORIZE_DUPLICATE_AS_DRAFT, function(AuthorizationCheckEvent $event) {
if ($event->element instanceof Entry) {
$event->authorized = false;
}
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
brandonkelly
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create a module with the following code: