Cannot clone a node containing a custom field with a 'UniqueField' custom constraint set on it

Created on 15 January 2025, 8 months ago

Problem/Motivation

The module does not look for fields with 'UniqueField' constraint set on them and tries to clone them as-is without updating such fields and that causes the clone process to fail on 'duplicate entry' error on DB level and the module to be unusable on such entities.

Steps to reproduce

1. Create a new content type or update an existing one.
2. Add a new field.
3. Set a 'UniqueField' constraint on it using code.
e.g.:
$fields['my_uuid'] = BaseFieldDefinition::create('uuid')
->setLabel(t('UUID'))
->addConstraint('UniqueField', [])
->setDefaultValue(FALSE)
->setReadOnly(TRUE);

4. Create a new node using the updated content type.
5. Try to use the module to clone it.

Proposed resolution

Detect fields with such a constraint and allow the user to clone the data of the original field with a change so it will stay unique and allowed to be cloned at the same time.

🐛 Bug report
Status

Active

Version

2.1

Component

Code

Created by

🇮🇱Israel ohadm

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @ohadm
  • 🇸🇮Slovenia deaom

    I don't think there is an issue with the UniqueField constraint and cloning, as when that is set to something that is not uuid, it works as expected.
    Tried setting the UniqueField constraint to user as in author, and that gets cloned with no issue (the user gets the cloned suffix added). When adding a content if the same user/author is added, it flags it as an error because of the constraint, meaning constraint is active.
    I think in this example the issue is in the uuid and how Drupal handles it. Also setting the field to read_only probably does not help in overwriting with a new value.
    Leaving open for somebody else to also have a look.

Production build 0.71.5 2024