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;
}
}
@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