Permalink
Browse files

ENHANCEMENT GridFieldDetailForm->setItemEditFormCallback()

  • Loading branch information...
1 parent fed2785 commit 72eadef113bc31e7b4fcfe396a34c61de7f6f20b @chillu chillu committed Apr 30, 2012
Showing with 37 additions and 0 deletions.
  1. +18 −0 forms/gridfield/GridFieldDetailForm.php
  2. +19 −0 tests/forms/gridfield/GridFieldDetailFormTest.php
@@ -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 {
@@ -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;
}
@@ -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 {

0 comments on commit 72eadef

Please sign in to comment.