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();
            }
        }
    }