Skip to content

Commit

Permalink
FIX Duplicate userforms using cascade_duplicates config
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Aug 21, 2024
1 parent 4c23cad commit 3cd2bd5
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 53 deletions.
38 changes: 9 additions & 29 deletions code/Extension/UserFormFieldEditorExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace SilverStripe\UserForms\Extension;

use SilverStripe\Admin\AdminRootController;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldPaginator;
use SilverStripe\Forms\Tab;
Expand Down Expand Up @@ -49,6 +50,10 @@ class UserFormFieldEditorExtension extends DataExtension
'Fields'
];

private static $cascade_duplicates = [
'Fields'
];

/**
* Adds the field editor to the page.
*
Expand Down Expand Up @@ -235,38 +240,13 @@ public function onAfterUnpublish()
* @param bool $doWrite
* @param string $manyMany
* @return DataObject
* @deprecated 5.3.0 Will be removed without equivalent functionality to replace it
*/
public function onAfterDuplicate($oldPage, $doWrite, $manyMany)
{
// List of EditableFieldGroups, where the key of the array is the ID of the old end group
$fieldGroups = [];
foreach ($oldPage->Fields() as $field) {
/** @var EditableFormField $newField */
$newField = $field->duplicate(false);
$newField->ParentID = $this->owner->ID;
$newField->ParentClass = $this->owner->ClassName;
$newField->Version = 0;
$newField->write();

// If we encounter a group start, record it for later use
if ($field instanceof EditableFieldGroup) {
$fieldGroups[$field->EndID] = $newField;
}

// If we encounter an end group, link it back to the group start
if ($field instanceof EditableFieldGroupEnd && isset($fieldGroups[$field->ID])) {
$groupStart = $fieldGroups[$field->ID];
$groupStart->EndID = $newField->ID;
$groupStart->write();
}

foreach ($field->DisplayRules() as $customRule) {
$newRule = $customRule->duplicate(false);
$newRule->ParentID = $newField->ID;
$newRule->Version = 0;
$newRule->write();
}
}
Deprecation::withNoReplacement(
fn() => Deprecation::notice('5.3.0', 'Will be removed without equivalent functionality to replace it')
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion code/Model/EditableFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ class EditableFormField extends DataObject
'DisplayRules',
];

private static $cascade_duplicates = false;
private static $cascade_duplicates = [
'DisplayRules',
];

/**
* This is protected rather that private so that it's unit testable
Expand Down
23 changes: 4 additions & 19 deletions code/Model/EditableFormField/EditableMultipleOptionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class EditableMultipleOptionField extends EditableFormField
'Options',
];

private static $cascade_duplicates = [
'Options',
];

private static $table_name = 'EditableMultipleOptionField';

/**
Expand Down Expand Up @@ -112,25 +116,6 @@ public function getCMSFields()
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
* {@link EditableDropdown} or {@link EditableRadioField}
Expand Down
4 changes: 4 additions & 0 deletions code/Model/Recipient/EmailRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class EmailRecipient extends DataObject
'CustomRules',
];

private static $cascade_duplicates = [
'CustomRules',
];

private static $summary_fields = [
'EmailAddress',
'EmailSubject',
Expand Down
9 changes: 5 additions & 4 deletions code/UserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ trait UserForm
'EmailRecipients',
];

private static $cascade_duplicates = false;
private static $cascade_duplicates = [
'EmailRecipients',
];

/**
* @var array
Expand Down Expand Up @@ -161,9 +163,8 @@ trait UserForm
private static $non_live_permissions = ['SITETREE_VIEW_ALL'];

/**
* Temporary storage of field ids when the form is duplicated.
* Example layout: array('EditableCheckbox3' => 'EditableCheckbox14')
* @var array
* Unused property
* @deprecated 5.3.0 Will be removed without equivalent functionality to replace it
*/
protected $fieldsFromTo = [];

Expand Down

0 comments on commit 3cd2bd5

Please sign in to comment.