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();
            }
        }
    }
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();
            }
        }
    }
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 ( ) ; } } }
When duplicating a UserDefinedForm, duplicate all of its fields and display rules

@see \SilverStripe\ORM\DataObject::duplicate
@param \SilverStripe\ORM\DataObject $oldPage
@param bool $doWrite
@param string $manyMany
@return \SilverStripe\ORM\DataObject
When duplicating a UserDefinedForm duplicate all of its fields and display rules