mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
FIX Add polymorphic class on new records for use in forms.
This commit is contained in:
parent
2836478c72
commit
5436df57e4
@ -18,6 +18,7 @@ use SilverStripe\ORM\DataObject;
|
||||
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 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler
|
||||
$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()) {
|
||||
|
@ -5,6 +5,7 @@ namespace SilverStripe\Forms\Tests\GridField;
|
||||
use SilverStripe\Control\Controller;
|
||||
use SilverStripe\Dev\CSSContentParser;
|
||||
use SilverStripe\Dev\FunctionalTest;
|
||||
use SilverStripe\Forms\Form;
|
||||
use SilverStripe\Forms\GridField\GridField;
|
||||
use SilverStripe\Forms\GridField\GridFieldDetailForm;
|
||||
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
|
||||
@ -14,6 +15,7 @@ use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\CategoryControlle
|
||||
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\GroupController;
|
||||
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PeopleGroup;
|
||||
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\Person;
|
||||
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\PolymorphicPeopleGroup;
|
||||
use SilverStripe\Forms\Tests\GridField\GridFieldDetailFormTest\TestController;
|
||||
|
||||
/**
|
||||
@ -26,6 +28,7 @@ class GridFieldDetailFormTest extends FunctionalTest
|
||||
protected static $extra_dataobjects = [
|
||||
Person::class,
|
||||
PeopleGroup::class,
|
||||
PolymorphicPeopleGroup::class,
|
||||
Category::class,
|
||||
];
|
||||
|
||||
@ -119,6 +122,30 @@ class GridFieldDetailFormTest extends FunctionalTest
|
||||
$this->assertEquals($count + 1, $group->People()->Count());
|
||||
}
|
||||
|
||||
public function testAddFormWithPolymorphicHasOne()
|
||||
{
|
||||
// Log in for permissions check
|
||||
$this->logInWithPermission('ADMIN');
|
||||
// Prepare gridfield and other objects
|
||||
$group = new PolymorphicPeopleGroup();
|
||||
$group->write();
|
||||
$gridField = $group->getCMSFields()->dataFieldByName('People');
|
||||
$gridField->setForm(new Form());
|
||||
$detailForm = $gridField->getConfig()->getComponentByType(GridFieldDetailForm::class);
|
||||
$record = new Person();
|
||||
|
||||
// Trigger creation of the item edit form
|
||||
$reflectionDetailForm = new \ReflectionClass($detailForm);
|
||||
$reflectionMethod = $reflectionDetailForm->getMethod('getItemRequestHandler');
|
||||
$reflectionMethod->setAccessible(true);
|
||||
$itemrequest = $reflectionMethod->invoke($detailForm, $gridField, $record, new Controller());
|
||||
$itemrequest->ItemEditForm();
|
||||
|
||||
// The polymorphic values should be pre-loaded
|
||||
$this->assertEquals(PolymorphicPeopleGroup::class, $record->PolymorphicGroupClass);
|
||||
$this->assertEquals($group->ID, $record->PolymorphicGroupID);
|
||||
}
|
||||
|
||||
public function testViewForm()
|
||||
{
|
||||
$this->logInWithPermission('ADMIN');
|
||||
|
@ -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 = [
|
||||
|
@ -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 PolymorphicPeopleGroup 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;
|
||||
}
|
||||
}
|
@ -110,4 +110,11 @@ class PolymorphicHasManyListTest extends SapphireTest
|
||||
$this->assertEmpty($subteam1fan->FavouriteID);
|
||||
$this->assertEmpty($subteam1fan->FavouriteClass);
|
||||
}
|
||||
|
||||
public function testGetForeignClassKey(): void
|
||||
{
|
||||
$team = $this->objFromFixture(DataObjectTest\Team::class, 'team1');
|
||||
$list = $team->Fans();
|
||||
$this->assertSame('FavouriteClass', $list->getForeignClassKey());
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user