Skip to content

Instantly share code, notes, and snippets.

@sunnysideup
Last active August 29, 2015 14:01
Show Gist options
  • Save sunnysideup/991aae5c8dd26d8b540c to your computer and use it in GitHub Desktop.
Save sunnysideup/991aae5c8dd26d8b540c to your computer and use it in GitHub Desktop.
pseudo code for class that allows you to customise the edit form for any data model
<?php
/**
* Extend this class to create a customised one record editor
* for your model - within the framework of the
* Silverstripe CMS.
*
* In the model class you identify the preferred CMS Editor Class.
*
* In the class below, you can set up custom CMS fields, actions (buttons)
* and the actions that relate to the buttons.
*
* Typically, you would use this class to add actions such as...
* - save and edit next
* - save and update third-party-API
* - save and publish related page
*
* Think of it like a controller for a model that is solely
* dedicated to editing the model in the CMS and its associated
* actions.
*
*
*/
abstract class DataObject_CMSEditor extends Controller {
private static $allowed_actions = array();
/**
* The model at hand that is being edited.
* @var String
*/
protected $model = null;
/**
* The record at hand that is being edited
* @var DataObject
*/
protected $record = null;
/**
* Set the model
* @param String $s
*/
public function setModel($s) {
$this->model = $s;
}
/**
* Set the model
* @param Object $o
*/
public function setRecord($o) {
$this->record = $o;
}
/**
* for the CMS fields, we take the fields from the model itself
* and we can change them here as and when required.
*
* @return FieldList
*/
public function getCMSFields() {
//for backward compatability - we add:
$fields = $this->model->getCMSFields();
//do stuff
return $fields;
}
/**
* returns the buttons that apply to the current
* model and record.
*
* @return FieldList
*/
public function getCMSActions(){
}
/**
* does something cool
*
* @param HTTPRequest
* @return ajax response
*/
public function myaction($request){
}
}
<?php
class MyModel extends DataObject {
/**
* name of the Class that manages the
* one-record editor in the CMS.
*
* @var String
*/
private static $cms_editor = "DataObject_CMSEditor";
/**
* return the class that manages the one record
* edits in the CMS.
* The environment parameter has been added
* for future proving. It is provided to add
* something like: modelAdmin environment, OR read-only environment
* or whatever.
*
* @param Mixed $situation
* @return String
*/
public function getCMSEditor($environment = null){
$editor = $this->Config()->get("cms_editor");
$extendedEditor = $this->extend("UpdateCMSEditor", $editor, $environment);
if($extendedEditor !== null) {
$editor = $extendedEditor;
}
return $editor;
}
}
@willmorgan
Copy link

The way this has turned out looks like we're completely removing the responsibility of CMS field generation from the model, which is good as it also makes it easier / less bloated to run the framework without the cms module.

IMHO, the models need to nominate the CMS editor, not the other way around. By default we can have a GenericCMSEditor which provides the same functionality as DataObject->getCMSFields, more or less.

Also, getCMSFields on $this->model will surely become redundant as the model delegates that responsibility to this DataObject_CMSEditor - so might as well remove the call.

@sunnysideup
Copy link
Author

@willmorgan - agree with everything you say ;-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment