Skip to content

Commit

Permalink
ENHANCEMENT GridFieldDetailForm->setItemEditFormCallback()
Browse files Browse the repository at this point in the history
  • Loading branch information
chillu committed Apr 30, 2012
1 parent fed2785 commit 72eadef
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
18 changes: 18 additions & 0 deletions forms/gridfield/GridFieldDetailForm.php
Expand Up @@ -147,6 +147,20 @@ public function getItemRequestClass() {
return 'GridFieldItemRequest_ItemRequest';
}
}

/**
* @param function $cb Make changes on the edit form after constructing it.
*/
public function setItemEditFormCallback($cb) {
$this->itemEditFormCallback = $cb;
}

/**
* @return function
*/
public function getItemEditFormCallback() {
return $this->itemEditFormCallback;
}
}

class GridFieldDetailForm_ItemRequest extends RequestHandler {
Expand Down Expand Up @@ -296,6 +310,10 @@ function ItemEditForm() {
// 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;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/forms/gridfield/GridFieldDetailFormTest.php
Expand Up @@ -133,6 +133,25 @@ function testCustomItemRequestClass() {
$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 {
Expand Down

0 comments on commit 72eadef

Please sign in to comment.