mirror of
https://github.com/silverstripe/silverstripe-userforms.git
synced 2024-10-22 17:05:42 +02:00
ENH Save modified form data when adding new field
This commit is contained in:
parent
ee2967722b
commit
d81da1eedf
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
58
tests/php/Form/GridFieldAddClassesButtonTest.php
Normal file
58
tests/php/Form/GridFieldAddClassesButtonTest.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace SilverStripe\UserForms\Tests\Form;
|
||||
|
||||
use SilverStripe\CMS\Controllers\CMSMain;
|
||||
use SilverStripe\Control\HTTPRequest;
|
||||
use SilverStripe\Control\Session;
|
||||
use SilverStripe\Core\Injector\Injector;
|
||||
use SilverStripe\Dev\SapphireTest;
|
||||
use SilverStripe\Forms\GridField\GridField;
|
||||
use SilverStripe\ORM\DataList;
|
||||
use SilverStripe\UserForms\Form\GridFieldAddClassesButton;
|
||||
use SilverStripe\UserForms\Model\EditableFormField;
|
||||
use SilverStripe\UserForms\Model\EditableFormField\EditableDateField;
|
||||
use SilverStripe\UserForms\Model\EditableFormField\EditableTextField;
|
||||
use SilverStripe\UserForms\Model\UserDefinedForm;
|
||||
use Symbiote\GridFieldExtensions\GridFieldEditableColumns;
|
||||
|
||||
class GridFieldAddClassesButtonTest extends SapphireTest
|
||||
{
|
||||
protected $usesDatabase = true;
|
||||
|
||||
public function testHandleAddUpdatesModifiedFormData()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user