Skip to content

Commit

Permalink
FIX Add polymorphic class on new records for use in forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed May 2, 2022
1 parent 2836478 commit faacbd9
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/Forms/GridField/GridFieldDetailForm_ItemRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down
19 changes: 19 additions & 0 deletions tests/php/Forms/GridField/GridFieldDetailFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
4 changes: 4 additions & 0 deletions tests/php/Forms/GridField/GridFieldDetailFormTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tests/php/Forms/GridField/GridFieldDetailFormTest/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest;

use SilverStripe\Dev\TestOnly;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\ORM\DataObject;

class PeopleGroup extends DataObject implements TestOnly
{
private static $table_name = 'GridFieldDetailFormTest_PolymorphicPeopleGroup';

private static $db = [
'Name' => '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;
}
}

0 comments on commit faacbd9

Please sign in to comment.