I have come up with a workaround as it was troublesome to get Save & Publish actions working in Model Admin. I bet with more time I could probably come up with the proper actions in EditForm() on my extension of ModelAdmin controller.... but the workaround is simple. It works by reloading the CMS to the SiteTree object (e.g. admin/show/[recordID]).
Below is the code. When time avails I'd like to submit a patch to the core ModelAdmin_Recordcontroller that will detect if the currentRecord->is_a('SiteTree') and behave accordingly. Is there any interest in this?
My problem with doing everything from SiteTree is that the interface gets troublesome (e.g. the create button often shows the wrong classes, clicking go creates multiple records, etc. etc.) -- which I presume will be cleaned up in the JS rewrite of 3.0.
Here is:
<?php
class NewsAdmin extends ModelAdmin {
public static $managed_models = array(
'NewsPost'
);
public static $record_controller_class = "NewsAdmin_RecordController";
static $url_segment = 'newsposts';
static $menu_title = 'News Posts';
}
class NewsAdmin_RecordController extends ModelAdmin_RecordController {
private function href(){
// TODO: properly detect CMS URL, do not assume "[base]/admin"
return Director::baseURL() . 'admin/show/' . $this->currentRecord->ID;
}
private function redirectHTML(){
return '<script type="text/javascript">window.location = "' . $this->href() . '";</script>' .
'Redirecting... please wait. <a href="' . $this->href() . '">Click here</a> to continue...';
}
function EditForm(){
return new Form(
$this,
"EditForm",
new FieldSet(
new LiteralField('redirect', $this->redirectHTML())
),
new FieldSet()
);
}
function edit($request) {
if ($this->currentRecord) {
$href = Director::baseURL() . 'admin/show/' . $this->currentRecord->ID;
return new SS_HTTPResponse(
$this->redirectHTML(),
200,
sprintf("Redirecting to '%s' for editing.",$this->currentRecord->Title)
);
} else {
return _t('ModelAdmin.ITEMNOTFOUND', "I can't find that item");
}
}
function view($request) {
if($this->currentRecord) {
$href = $this->href();
return new SS_HTTPResponse(
$this->redirectHTML(),
200,
sprintf("Redirecting to '%s' for viewing.",$this->currentRecord->Title)
);
} else {
return _t('ModelAdmin.ITEMNOTFOUND', "I can't find that item");
}
}
}