ENHANCEMENT Prepopulating group on new members (fixes #7217)

This commit is contained in:
Ingo Schommer 2012-04-30 14:20:21 +02:00
parent 72eadef113
commit f873707e9d
2 changed files with 15 additions and 4 deletions

View File

@ -149,14 +149,14 @@ class GridFieldDetailForm implements GridField_URLHandler {
} }
/** /**
* @param function $cb Make changes on the edit form after constructing it. * @param Closure $cb Make changes on the edit form after constructing it.
*/ */
public function setItemEditFormCallback($cb) { public function setItemEditFormCallback(Closure $cb) {
$this->itemEditFormCallback = $cb; $this->itemEditFormCallback = $cb;
} }
/** /**
* @return function * @return Closure
*/ */
public function getItemEditFormCallback() { public function getItemEditFormCallback() {
return $this->itemEditFormCallback; return $this->itemEditFormCallback;

View File

@ -94,12 +94,23 @@ class Group extends DataObject {
$permissionsField->setHiddenPermissions(SecurityAdmin::$hidden_permissions); $permissionsField->setHiddenPermissions(SecurityAdmin::$hidden_permissions);
if($this->ID) { if($this->ID) {
$group = $this;
$config = new GridFieldConfig_RelationEditor(); $config = new GridFieldConfig_RelationEditor();
$config->addComponents(new GridFieldExportButton('before')); $config->addComponents(new GridFieldExportButton('before'));
$config->addComponents(new GridFieldPrintButton('before')); $config->addComponents(new GridFieldPrintButton('before'));
$config->getComponentByType('GridFieldAddExistingAutocompleter') $config->getComponentByType('GridFieldAddExistingAutocompleter')
->setResultsFormat('$Title ($Email)')->setSearchFields(array('FirstName', 'Surname', 'Email')); ->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'); $memberList = GridField::create('Members',false, $this->Members(), $config)->addExtraClass('members_grid');
// @todo Implement permission checking on GridField // @todo Implement permission checking on GridField
//$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd')); //$memberList->setPermissions(array('edit', 'delete', 'export', 'add', 'inlineadd'));