Reputation: 5875
I'd like to create a custom GridField action that will open a form in a popup. Here's what I have:
<?php
class CustomItemRequest extends GridFieldDetailForm_ItemRequest
{
private static $allowed_actions = array(
'edit',
'view',
'ItemEditForm',
'customaction',
'CustomForm',
);
public function customaction($request) {
return $this->CustomForm()->forTemplate();
}
public function CustomForm()
{
$fields = new FieldList(
TextField::create('Name', 'Your Name')
);
$actions = new FieldList(
FormAction::create('doCustomAction')
);
$form = new Form($this, 'CustomForm', $fields, $actions);
return $form;
}
public function doCustomAction($data, $form, $request)
{
}
}
On the GridFieldConfig I set the custom request handler like this:
$config->getComponentByType('GridFieldDetailForm')
->setItemRequestClass('CustomItemRequest');
And then I wire everything up with a custom GridField_ColumnProvider
that renders a link to the GridField. I use some custom Javascript to load the form in a popup:
$('.cms a.my-custom-link').entwine({
onclick: function (e) {
e.preventDefault();
var dialog = $('<div class="custom-dialog"/>');
dialog.ssdialog({iframeUrl: this.attr('href'), height: 300});
dialog.ssdialog('open');
}
});
So far, everything works fine. I can click the action-link in the GridField and the form opens up in the popup. However, there seems to be an issue with the routing, as the form adds its action to the current GridField action upon submitting. This results in an URL like this:
http://localhost/admin/modeladmin/CustomDataObject/EditForm/field/CustomDataObject/item/1/admin/modeladmin/CustomDataObject/EditForm/field/CustomDataObject/item/1/CustomForm
And this triggers the following error:
Action 'admin' isn't available on class CustomItemRequest.
How can I make the Form routing work in this case? Do I need a custom controller and not use GridFieldDetailForm_ItemRequest
at all?
Upvotes: 1
Views: 362
Reputation: 5875
Well, the problem is actually rather easy to solve.
Instead of:
public function customaction($request) {
return $this->CustomForm()->forTemplate();
}
You should be doing:
public function customaction($request) {
return $this->renderWith('MyCustomTemplate');
}
And then MyCustomTemplate.ss
should look like this:
<!doctype html>
<head>
<% base_tag %>
</head>
<body>
$CustomForm
</body>
</html>
The key part is the inclusion of <% base_tag %>
, so that the relative Form-Action URL works as it should.
Upvotes: 1