From faacbd9bdb74af0060b811fb02388955b8499d35 Mon Sep 17 00:00:00 2001 From: Guy Sartorelli Date: Mon, 2 May 2022 18:25:20 +1200 Subject: [PATCH] FIX Add polymorphic class on new records for use in forms. --- .../GridFieldDetailForm_ItemRequest.php | 7 ++++ .../GridField/GridFieldDetailFormTest.php | 19 ++++++++++ .../GridField/GridFieldDetailFormTest.yml | 4 ++ .../GridFieldDetailFormTest/Person.php | 3 +- .../PolymorphicPeopleGroup.php | 38 +++++++++++++++++++ 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/php/Forms/GridField/GridFieldDetailFormTest/PolymorphicPeopleGroup.php diff --git a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php index 008ef845f14..02b96b8be31 100644 --- a/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php +++ b/src/Forms/GridField/GridFieldDetailForm_ItemRequest.php @@ -18,6 +18,7 @@ use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\ORM\HasManyList; use SilverStripe\ORM\ManyManyList; +use SilverStripe\ORM\PolymorphicHasManyList; use SilverStripe\ORM\RelationList; use SilverStripe\ORM\SS_List; use SilverStripe\ORM\ValidationException; @@ -201,6 +202,12 @@ public function ItemEditForm() $key = $list->getForeignKey(); $id = $list->getForeignID(); $this->record->$key = $id; + // If the list is polymorphic, add the foreign class as well. + if ($list instanceof PolymorphicHasManyList) { + $classKey = $list->getForeignClassKey(); + $class = $list->getForeignClass(); + $this->record->$classKey = $class; + } } if (!$this->record->canView()) { diff --git a/tests/php/Forms/GridField/GridFieldDetailFormTest.php b/tests/php/Forms/GridField/GridFieldDetailFormTest.php index b4e1b7e12d5..ce7bd386f0f 100644 --- a/tests/php/Forms/GridField/GridFieldDetailFormTest.php +++ b/tests/php/Forms/GridField/GridFieldDetailFormTest.php @@ -119,6 +119,25 @@ public function testAddForm() $this->assertEquals($count + 1, $group->People()->Count()); } + public function testAddFormWithPolymorphicHasOne() + { + $group = PolymorphicPeopleGroup::get() + ->filter('Name', 'My Group') + ->sort('Name') + ->First(); + $gridField = $group->getCMSFields()->dataFieldByName('People'); + $detailForm = $gridField->getConfig()->getComponentByType(GridFieldDetailForm::class); + + $reflectionDetailForm = new ReflectionClass($detailForm); + $reflectionMethod = $reflectionDetailForm->getMethod('getItemRequestHandler'); + $reflectionMethod->setAccessible(true); + $itemrequest = $reflectionMethod->invoke($detailForm, [$gridField, $record, new Controller()]); + $itemrequest->ItemEditForm(); + + $this->assertEquals(PolymorphicPeopleGroup::class, $record->PolymorphicGroupClass); + $this->assertEquals(PolymorphicPeopleGroup::class, $record->PolymorphicGroupID); + } + public function testViewForm() { $this->logInWithPermission('ADMIN'); diff --git a/tests/php/Forms/GridField/GridFieldDetailFormTest.yml b/tests/php/Forms/GridField/GridFieldDetailFormTest.yml index 2182cc031ec..84a89142d95 100644 --- a/tests/php/Forms/GridField/GridFieldDetailFormTest.yml +++ b/tests/php/Forms/GridField/GridFieldDetailFormTest.yml @@ -16,6 +16,10 @@ SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PeopleGroup: - =>SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Person.joe - =>SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Person.jane +SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PolymorphicPeopleGroup: + group: + Name: My Group + SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Category: category1: Name: Category 1 diff --git a/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php b/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php index 0caf52d1c9d..9b76b3d2c23 100644 --- a/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php +++ b/tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php @@ -18,7 +18,8 @@ class Person extends DataObject implements TestOnly ]; private static $has_one = [ - 'Group' => PeopleGroup::class + 'Group' => PeopleGroup::class, + 'PolymorphicGroup' => DataObject::class, ]; private static $many_many = [ diff --git a/tests/php/Forms/GridField/GridFieldDetailFormTest/PolymorphicPeopleGroup.php b/tests/php/Forms/GridField/GridFieldDetailFormTest/PolymorphicPeopleGroup.php new file mode 100644 index 00000000000..fd1033a8dfc --- /dev/null +++ b/tests/php/Forms/GridField/GridFieldDetailFormTest/PolymorphicPeopleGroup.php @@ -0,0 +1,38 @@ + 'Varchar' + ]; + + private static $has_many = [ + 'People' => Person::class + ]; + + private static $default_sort = '"Name"'; + + public function getCMSFields() + { + $fields = parent::getCMSFields(); + $fields->replaceField( + 'People', + GridField::create( + 'People', + 'People', + $this->People(), + GridFieldConfig_RelationEditor::create() + ) + ); + return $fields; + } +}