mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Merge pull request #382 from chillu/trac/7217-group-default-on-member
Group default on member (#7217)
This commit is contained in:
commit
6483cdd204
@ -28,6 +28,16 @@ class GridFieldDetailForm implements GridField_URLHandler {
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* @var String
|
||||
*/
|
||||
protected $itemRequestClass;
|
||||
|
||||
/**
|
||||
* @var function With two parameters: $form and $component
|
||||
*/
|
||||
protected $itemEditFormCallback;
|
||||
|
||||
function getURLHandlers($gridField) {
|
||||
return array(
|
||||
'item/$ID' => 'handleItem',
|
||||
@ -64,11 +74,7 @@ class GridFieldDetailForm implements GridField_URLHandler {
|
||||
$record = Object::create($gridField->getModelClass());
|
||||
}
|
||||
|
||||
if(ClassInfo::exists(get_class($this) . "_ItemRequest")) {
|
||||
$class = get_class($this) . "_ItemRequest";
|
||||
} else {
|
||||
$class = 'GridFieldDetailForm_ItemRequest';
|
||||
}
|
||||
$class = $this->getItemRequestClass();
|
||||
|
||||
$handler = Object::create($class, $gridField, $this, $record, $controller, $this->name);
|
||||
$handler->setTemplate($this->template);
|
||||
@ -120,6 +126,41 @@ class GridFieldDetailForm implements GridField_URLHandler {
|
||||
public function getValidator() {
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param String
|
||||
*/
|
||||
public function setItemRequestClass($class) {
|
||||
$this->itemRequestClass = $class;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return String
|
||||
*/
|
||||
public function getItemRequestClass() {
|
||||
if($this->itemRequestClass) {
|
||||
return $this->itemRequestClass;
|
||||
} else if(ClassInfo::exists(get_class($this) . "_ItemRequest")) {
|
||||
return get_class($this) . "_ItemRequest";
|
||||
} else {
|
||||
return 'GridFieldItemRequest_ItemRequest';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Closure $cb Make changes on the edit form after constructing it.
|
||||
*/
|
||||
public function setItemEditFormCallback(Closure $cb) {
|
||||
$this->itemEditFormCallback = $cb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Closure
|
||||
*/
|
||||
public function getItemEditFormCallback() {
|
||||
return $this->itemEditFormCallback;
|
||||
}
|
||||
}
|
||||
|
||||
class GridFieldDetailForm_ItemRequest extends RequestHandler {
|
||||
@ -269,6 +310,10 @@ class GridFieldDetailForm_ItemRequest extends RequestHandler {
|
||||
// e.g. page/edit/show/6/ vs. page/edit/EditForm/field/MyGridField/....
|
||||
$form->Backlink = $toplevelController->Link();
|
||||
}
|
||||
|
||||
$cb = $this->component->getItemEditFormCallback();
|
||||
if($cb) $cb($form, $this);
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
|
@ -94,12 +94,23 @@ class Group extends DataObject {
|
||||
$permissionsField->setHiddenPermissions(SecurityAdmin::$hidden_permissions);
|
||||
|
||||
if($this->ID) {
|
||||
$group = $this;
|
||||
$config = new GridFieldConfig_RelationEditor();
|
||||
$config->addComponents(new GridFieldExportButton('before'));
|
||||
$config->addComponents(new GridFieldPrintButton('before'));
|
||||
$config->getComponentByType('GridFieldAddExistingAutocompleter')
|
||||
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email'));
|
||||
$config->getComponentByType('GridFieldDetailForm')->setValidator(new Member_Validator());
|
||||
$config->getComponentByType('GridFieldDetailForm')
|
||||
->setValidator(new Member_Validator())
|
||||
->setItemEditFormCallback(function($form, $component) use($group) {
|
||||
// If new records are created in a group context,
|
||||
// set this group by default.
|
||||
$record = $form->getRecord();
|
||||
if($record && !$record->ID) {
|
||||
$groupsField = $form->Fields()->dataFieldByName('DirectGroups');
|
||||
if($groupsField) $groupsField->setValue($group->ID);
|
||||
}
|
||||
});
|
||||
$memberList = GridField::create('Members',false, $this->Members(), $config)->addExtraClass('members_grid');
|
||||
// @todo Implement permission checking on GridField
|
||||
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));
|
||||
|
@ -126,6 +126,32 @@ class GridFieldDetailFormTest extends FunctionalTest {
|
||||
|
||||
// Fourth level form would be a Category detail view
|
||||
}
|
||||
|
||||
function testCustomItemRequestClass() {
|
||||
$component = new GridFieldDetailForm();
|
||||
$this->assertEquals('GridFieldDetailForm_ItemRequest', $component->getItemRequestClass());
|
||||
$component->setItemRequestClass('GridFieldDetailFormTest_ItemRequest');
|
||||
$this->assertEquals('GridFieldDetailFormTest_ItemRequest', $component->getItemRequestClass());
|
||||
}
|
||||
|
||||
function testItemEditFormCallback() {
|
||||
$category = new GridFieldDetailFormTest_Category();
|
||||
$component = new GridFieldDetailForm();
|
||||
$component->setItemEditFormCallback(function($form, $component) {
|
||||
$form->Fields()->push(new HiddenField('Callback'));
|
||||
});
|
||||
// Note: A lot of scaffolding to execute the tested logic,
|
||||
// due to the coupling of form creation with request handling (and its context)
|
||||
$request = new GridFieldDetailForm_ItemRequest(
|
||||
GridField::create('Categories', 'Categories'),
|
||||
$component,
|
||||
$category,
|
||||
new Controller(),
|
||||
'Form'
|
||||
);
|
||||
$form = $request->ItemEditForm();
|
||||
$this->assertNotNull($form->Fields()->fieldByName('Callback'));
|
||||
}
|
||||
}
|
||||
|
||||
class GridFieldDetailFormTest_Person extends DataObject implements TestOnly {
|
||||
@ -230,3 +256,6 @@ class GridFieldDetailFormTest_GroupController extends Controller implements Test
|
||||
return new Form($this, 'Form', new FieldList($field), new FieldList());
|
||||
}
|
||||
}
|
||||
|
||||
class GridFieldDetailFormTest_ItemRequest extends GridFieldDetailForm_ItemRequest implements TestOnly {
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user