FIX Duplicate userforms using cascade_duplicates config (#1320)

This commit is contained in:
Guy Sartorelli 2024-08-28 11:32:21 +12:00 committed by GitHub
parent 4c23cadc9e
commit db8adf5890
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 25 additions and 40 deletions

View File

@ -42,6 +42,8 @@ class UserFormFieldEditorExtension extends DataExtension
); );
private static $owns = [ private static $owns = [
// Note we explicitly cannot use $cascade_duplicates to duplicate this relation.
// See onAfterDuplicate()
'Fields' 'Fields'
]; ];
@ -228,13 +230,14 @@ class UserFormFieldEditorExtension extends DataExtension
} }
/** /**
* When duplicating a UserDefinedForm, duplicate all of its fields and display rules * Duplicate the Fields() relation.
* When duplicating a UserDefinedForm, ensure the group ends aren't duplicated twice,
* but also ensure they are connected to the correct duplicated Group.
* *
* @see DataObject::duplicate * @see DataObject::duplicate
* @param DataObject $oldPage * @param DataObject $oldPage
* @param bool $doWrite * @param bool $doWrite
* @param string $manyMany * @param null|array $manyMany
* @return DataObject
*/ */
public function onAfterDuplicate($oldPage, $doWrite, $manyMany) public function onAfterDuplicate($oldPage, $doWrite, $manyMany)
{ {
@ -243,10 +246,7 @@ class UserFormFieldEditorExtension extends DataExtension
foreach ($oldPage->Fields() as $field) { foreach ($oldPage->Fields() as $field) {
/** @var EditableFormField $newField */ /** @var EditableFormField $newField */
$newField = $field->duplicate(false); $newField = $field->duplicate(false);
$newField->ParentID = $this->owner->ID; $this->getOwner()->Fields()->add($newField);
$newField->ParentClass = $this->owner->ClassName;
$newField->Version = 0;
$newField->write();
// If we encounter a group start, record it for later use // If we encounter a group start, record it for later use
if ($field instanceof EditableFieldGroup) { if ($field instanceof EditableFieldGroup) {
@ -259,13 +259,6 @@ class UserFormFieldEditorExtension extends DataExtension
$groupStart->EndID = $newField->ID; $groupStart->EndID = $newField->ID;
$groupStart->write(); $groupStart->write();
} }
foreach ($field->DisplayRules() as $customRule) {
$newRule = $customRule->duplicate(false);
$newRule->ParentID = $newField->ID;
$newRule->Version = 0;
$newRule->write();
}
} }
} }

View File

@ -181,7 +181,9 @@ class EditableFormField extends DataObject
'DisplayRules', 'DisplayRules',
]; ];
private static $cascade_duplicates = false; private static $cascade_duplicates = [
'DisplayRules',
];
/** /**
* This is protected rather that private so that it's unit testable * This is protected rather that private so that it's unit testable

View File

@ -55,6 +55,10 @@ class EditableMultipleOptionField extends EditableFormField
'Options', 'Options',
]; ];
private static $cascade_duplicates = [
'Options',
];
private static $table_name = 'EditableMultipleOptionField'; private static $table_name = 'EditableMultipleOptionField';
/** /**
@ -112,25 +116,6 @@ class EditableMultipleOptionField extends EditableFormField
return $fields; return $fields;
} }
/**
* Duplicate a pages content. We need to make sure all the fields attached
* to that page go with it
* {@inheritDoc}
*/
public function duplicate(bool $doWrite = true, array|null $relations = null): static
{
$clonedNode = parent::duplicate(true);
foreach ($this->Options() as $field) {
$newField = $field->duplicate(false);
$newField->ParentID = $clonedNode->ID;
$newField->Version = 0;
$newField->write();
}
return $clonedNode;
}
/** /**
* Return whether or not this field has addable options such as a * Return whether or not this field has addable options such as a
* {@link EditableDropdown} or {@link EditableRadioField} * {@link EditableDropdown} or {@link EditableRadioField}

View File

@ -103,6 +103,10 @@ class EmailRecipient extends DataObject
'CustomRules', 'CustomRules',
]; ];
private static $cascade_duplicates = [
'CustomRules',
];
private static $summary_fields = [ private static $summary_fields = [
'EmailAddress', 'EmailAddress',
'EmailSubject', 'EmailSubject',

View File

@ -125,7 +125,9 @@ trait UserForm
'EmailRecipients', 'EmailRecipients',
]; ];
private static $cascade_duplicates = false; private static $cascade_duplicates = [
'EmailRecipients',
];
/** /**
* @var array * @var array
@ -161,9 +163,8 @@ trait UserForm
private static $non_live_permissions = ['SITETREE_VIEW_ALL']; private static $non_live_permissions = ['SITETREE_VIEW_ALL'];
/** /**
* Temporary storage of field ids when the form is duplicated. * Unused property
* Example layout: array('EditableCheckbox3' => 'EditableCheckbox14') * @deprecated 5.3.0 Will be removed without equivalent functionality to replace it
* @var array
*/ */
protected $fieldsFromTo = []; protected $fieldsFromTo = [];

View File

@ -363,7 +363,7 @@ class UserDefinedFormTest extends FunctionalTest
$form2 = $this->objFromFixture(UserDefinedForm::class, 'page-with-group'); $form2 = $this->objFromFixture(UserDefinedForm::class, 'page-with-group');
$form2Validator = new UserFormValidator(); $form2Validator = new UserFormValidator();
$form2Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList())); $form2Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
$this->assertTrue($form2Validator->php($form2->toMap())); $this->assertTrue($form2Validator->php($form2->toMap()), json_encode($form2Validator->getResult()->getMessages()));
// Check field groups exist // Check field groups exist
$form2GroupStart = $form2->Fields()->filter('ClassName', EditableFieldGroup::class)->first(); $form2GroupStart = $form2->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
@ -374,7 +374,7 @@ class UserDefinedFormTest extends FunctionalTest
$form3 = $form2->duplicate(); $form3 = $form2->duplicate();
$form3Validator = new UserFormValidator(); $form3Validator = new UserFormValidator();
$form3Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList())); $form3Validator->setForm(new Form(new Controller(), Form::class, new FieldList(), new FieldList()));
$this->assertTrue($form3Validator->php($form3->toMap())); $this->assertTrue($form3Validator->php($form3->toMap()), json_encode($form3Validator->getResult()->getMessages()));
// Check field groups exist // Check field groups exist
$form3GroupStart = $form3->Fields()->filter('ClassName', EditableFieldGroup::class)->first(); $form3GroupStart = $form3->Fields()->filter('ClassName', EditableFieldGroup::class)->first();
$form3GroupEnd = $form3->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first(); $form3GroupEnd = $form3->Fields()->filter('ClassName', EditableFieldGroupEnd::class)->first();