From d81da1eedf8652845dcbbf3451ed22fbbffc9f52 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 23 May 2022 14:01:31 +1200 Subject: [PATCH] ENH Save modified form data when adding new field --- code/Form/GridFieldAddClassesButton.php | 35 +++++++++++ .../Form/GridFieldAddClassesButtonTest.php | 58 +++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 tests/php/Form/GridFieldAddClassesButtonTest.php diff --git a/code/Form/GridFieldAddClassesButton.php b/code/Form/GridFieldAddClassesButton.php index ae79a0c..44400fc 100644 --- a/code/Form/GridFieldAddClassesButton.php +++ b/code/Form/GridFieldAddClassesButton.php @@ -7,6 +7,8 @@ use SilverStripe\Forms\GridField\GridField; use SilverStripe\Forms\GridField\GridField_ActionProvider; use SilverStripe\Forms\GridField\GridField_FormAction; use SilverStripe\Forms\GridField\GridField_HTMLProvider; +use SilverStripe\UserForms\Model\EditableFormField; +use Symbiote\GridFieldExtensions\GridFieldEditableColumns; /** * A button which allows objects to be created with a specified classname(s) @@ -248,7 +250,40 @@ class GridFieldAddClassesButton implements GridField_HTMLProvider, GridField_Act $list->add($item); } + $this->writeModifiedFieldData($grid); + // Should trigger a simple reload return null; } + + /** + * Write any existing form field data that was modified before clicking add button + */ + private function writeModifiedFieldData(GridField $gridField): void + { + $vars = $gridField->getRequest()->requestVars(); + $data = $vars['Fields'][GridFieldEditableColumns::POST_KEY] ?? []; + $ids = array_keys($data); + if (empty($ids)) { + return; + } + $list = EditableFormField::get()->filter(['ID' => $ids]); + foreach ($ids as $id) { + $row = $data[$id]; + $field = $list->byID($id); + if (!$field) { + continue; + } + $update = false; + foreach ($row as $name => $value) { + if ($field->{$name} !== $value) { + $field->{$name} = $value; + $update = true; + } + } + if ($update) { + $field->write(); + } + } + } } diff --git a/tests/php/Form/GridFieldAddClassesButtonTest.php b/tests/php/Form/GridFieldAddClassesButtonTest.php new file mode 100644 index 0000000..4dffc02 --- /dev/null +++ b/tests/php/Form/GridFieldAddClassesButtonTest.php @@ -0,0 +1,58 @@ +logInWithPermission('SITETREE_EDIT_ALL'); + $udf = UserDefinedForm::create(['Title' => 'MyUDF']); + $udfID = $udf->write(); + // Set the current controller to CMSMain to satisfy EditableFormField::getCanCreateContext() + /** @var CMSMain $controller */ + $controller = Injector::inst()->get(CMSMain::class); + $request = new HTTPRequest('GET', '/'); + $request->setSession(new Session([])); + $controller->setRequest($request); + $controller->setCurrentPageID($udf->ID); + $controller->pushCurrent(); + $list = new DataList(EditableFormField::class); + $field = EditableTextField::create(['ParentID' => $udfID, 'Title' => 'MyTitle']); + $fieldID = $field->write(); + $list->add($field); + $gridField = new GridField('MyName', 'MyTitle', $list); + $button = new GridFieldAddClassesButton([EditableTextField::class]); + $request = new HTTPRequest('POST', 'url', [], [ + 'Fields' => [ + GridFieldEditableColumns::POST_KEY => [ + $fieldID => [ + 'ClassName' => EditableDateField::class, + 'Title' => 'UpdatedTitle' + ] + ] + ] + ]); + $gridField->setRequest($request); + $button->handleAdd($gridField); + $field = EditableFormField::get()->byID($fieldID); + $this->assertSame(EditableDateField::class, $field->ClassName); + $this->assertSame('UpdatedTitle', $field->Title); + } +}