The answer above doesn't work anymore in Craft 3.7. While duplicated entries are disabled using this hook, saving an existing entry also disables it (even when usingUsing ElementHelper::isDraftOrRevisionduplicateOf
is not sufficient to catch some edge-cases)detect duplicates. Probably relatedA draft, provisional draft or revision being applied to the workflow changes introducedlive entry will also have a duplicateOf
property, so this will result in Craft 3.7. I've opened anlive entries being disabled on save (see this issue on Github about thisfor reference).
Arguably,To be sure the hook only applies to duplicated entries being disabled or marked as drafts should be, you also need to check the defaultupdatingFromDerivative
property – if that is false, the entry is being duplicated. I've create
Here's a complete working hook for Craft 3.7:
use craft\elements\Entry;
use craft\events\ModelEvent;
use craft\helpers\ElementHelper;
Event::on(
Entry::class,
Entry::EVENT_BEFORE_SAVE,
function (ModelEvent $e) {
$entry = $e->sender;
if (ElementHelper::isDraftOrRevision($entry)) return;
if (!$entry->duplicateOf || $entry->updatingFromDerivative) return;
$entry->enabled = false;
}
);
In Craft 4, duplicated entries will be disabled by default (see the discussion for this feature requeston Github. If you care about this feature being implemented, upvote it on Github for visibilityreference).