Skip to main content
update with working hook for 3.7+
Source Link
MoritzLost
  • 11.9k
  • 6
  • 23

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).

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 using ElementHelper::isDraftOrRevision to catch some edge-cases). Probably related to the workflow changes introduced in Craft 3.7. I've opened an issue on Github about this.

Arguably, duplicated entries being disabled or marked as drafts should be the default. I've create a discussion for this feature request. If you care about this feature being implemented, upvote it on Github for visibility.

Using duplicateOf is not sufficient to detect duplicates. A draft, provisional draft or revision being applied to the live entry will also have a duplicateOf property, so this will result in live entries being disabled on save (see this issue on Github for reference).

To be sure the hook only applies to duplicated entries, you also need to check the updatingFromDerivative property – if that is false, the entry is being duplicated.

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 on Github for reference).

Source Link
MoritzLost
  • 11.9k
  • 6
  • 23

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 using ElementHelper::isDraftOrRevision to catch some edge-cases). Probably related to the workflow changes introduced in Craft 3.7. I've opened an issue on Github about this.

Arguably, duplicated entries being disabled or marked as drafts should be the default. I've create a discussion for this feature request. If you care about this feature being implemented, upvote it on Github for visibility.