mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge pull request #2663 from ajshort/has-many-prepopulate
NEW: Pre-populate and disable foreign key field on has many lists.
This commit is contained in:
commit
7e291133a0
@ -372,8 +372,26 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
|
||||
$actions->push(new LiteralField('cancelbutton', $text));
|
||||
}
|
||||
}
|
||||
|
||||
$fields = $this->component->getFields();
|
||||
if(!$fields) $fields = $this->record->getCMSFields();
|
||||
|
||||
// If we are creating a new record in a has-many list, then
|
||||
// pre-populate the record's foreign key. Also disable the form field as
|
||||
// it has no effect.
|
||||
if($list instanceof HasManyList) {
|
||||
$key = $list->getForeignKey();
|
||||
$id = $list->getForeignID();
|
||||
|
||||
if(!$this->record->isInDB()) {
|
||||
$this->record->$key = $id;
|
||||
}
|
||||
|
||||
if($field = $fields->dataFieldByName($key)) {
|
||||
$fields->makeFieldReadonly($field);
|
||||
}
|
||||
}
|
||||
|
||||
$form = new Form(
|
||||
$this,
|
||||
'ItemEditForm',
|
||||
|
@ -220,6 +220,31 @@ class GridFieldDetailFormTest extends FunctionalTest {
|
||||
$form = $request->ItemEditForm();
|
||||
$this->assertNotNull($form->Fields()->fieldByName('Callback'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that a has-many detail form is pre-populated with the parent ID.
|
||||
*/
|
||||
public function testHasManyFormPrePopulated() {
|
||||
$group = $this->objFromFixture(
|
||||
'GridFieldDetailFormTest_PeopleGroup', 'group'
|
||||
);
|
||||
|
||||
$this->logInWithPermission('ADMIN');
|
||||
|
||||
$response = $this->get('GridFieldDetailFormTest_Controller');
|
||||
$parser = new CSSContentParser($response->getBody());
|
||||
$addLink = $parser->getBySelector('.ss-gridfield .new-link');
|
||||
$addLink = (string) $addLink[0]['href'];
|
||||
|
||||
$response = $this->get($addLink);
|
||||
$parser = new CSSContentParser($response->getBody());
|
||||
$title = $parser->getBySelector('#GroupID span');
|
||||
$id = $parser->getBySelector('#GroupID input');
|
||||
|
||||
$this->assertEquals($group->Name, (string) $title[0]);
|
||||
$this->assertEquals($group->ID, (string) $id[0]['value']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
|
||||
|
Loading…
Reference in New Issue
Block a user