ModelAdmin - some simple snippets


SilverStripe documentation  -  SilverStripe  ModelAdmin API  -  LeftAndMain 'Taming the beast'

tabs or dropdown...

By default SilverStripe shows the managed models as tabs. If you want to use a dropdown instead, add the following to your ModelAdmin subclass:

public function SearchClassSelector() {
	return "dropdown";

Note: if you want to implement the LeftAndMain RemodelAdmin class (see link above) you need to add this. The JavaScript binds list view actions to the #ModelClassSelector, and that one only exists in the 'dropdown' version.

Enable/disable CSV import

To disable CSV import for all managed models, put this in your ModelAdmin subclass:

public $showImportForm = false; 

You can also use an array of models for which you want CSV import to be enabled:

public $showImportForm = array(

Remove a field from the EditForm

If you don't add a getCMSField() function to your managed model, SilverStripe will scaffold the EditForm for you. If you wanted to remove one of the fields - say 'Description', you can simply do this:

function getCMSFields() {
	$fields = parent::getCMSFields();
	return $fields;

Manipulate searchfields

If you are using searchable fields, you have some freedom to decide what type of field you want to use for it in the ModelAdmin Search box:

static $searchable_fields = array(
	"Organisation.ZipCode" => array(
		"field" => "TextField",
		"filter" => "PartialMatchFilter",
		"title" => 'Organisation ZIP'

field - Afaik only TextField and DropdownField are supported. It's a great option, for instance when you are using an SS_DateTime field, because it generates a composite field that you should definitely replace by a TextField! 

filter - The PartialMatchFileter Class performs a LIKE '%searchword%' search on the database field - you don't really have to set it, because it's the default searchfilter that is already set in the static DBField::$default_search_filter_class variable. But you could set your own searchfilter this way. There is a (base) SearchFilter class, that you could extend, like the PartialMatchFileter does.

title - Using 'Title' like this will not work very wel with different locales. Check out this link to see how to translate field labels...

Field formatting and field casting

You can always determine how column values are rendered in a TableListField by using its setFieldFormatting() and setFieldCasting() methods. If you want to accomplish this for the TLF in a ModelAdmin subclass, you can do that by subclassing the getResultsTable() method, that returns the TLF. There's just one little thing: it doesn't live in the ModelAdmin class, but in theModeladmin_CollectionController class, so we need to subclass it as well.

Suppose we have a ProductAdmin class that manages a Product DataObject. We now need the following two classes:

class Product_Admin extends ModelAdmin {

	static $menu_title = "Products";
	static $url_segment = "products";

	static $managed_models = array(

	static $allowed_actions = array(

	tell ModelAdmin what CollectionController to use
	public static $collection_controller_class = "Product_CollectionController";

class Product_CollectionController extends ModelAdmin_CollectionController {

	function getResultsTable($searchCriteria) {
		$tf = parent::getResultsTable($searchCriteria);
		$tf->setFieldFormatting(array('Name' => "*** \$Name ***"));
		$tf->setFieldCasting(array('Price' => 'Currency->Nice'));
		return $tf;

This example will format a productname like "*** my product ***", and the price will be casted to a Currency datatype, and formatted by it's Nice() method.

  • Field formatting: create a HTML 'template' for the fieldvalue, that can be eval'd. You can combine the original fieldvalue with other field values to make things more complex. Keep in mind that the field formatting string wil be eval'd, so it should have the right syntax. 
  • Field Casting: cast the field to some other datatype, so you can use it's methods to format the end result.

CSV Export for all fields - not just the summaryfields

By default the CSV export button in a TableListFieldwill only export the summaryfields. To change that behaviour you need to use its setFieldListCsv() method to set an array of the fields you want to export. Just like in the formatting/casting example above, you can extend the ModelAdmin's CollectionController class to get a hold of the TableListField:

class Product_CollectionController extends ModelAdmin_CollectionController {

	function getResultsTable($searchCriteria) {
		$tf = parent::getResultsTable($searchCriteria);

			'Name' => 'Name',
			'Price' => 'Price',
			'Code' => 'code'

		return $tf;

This will make ModelAdmin export the Product's fields Name, Price and Code to CSV.


  • Thanks for the info and code examples regarding changing the CSV exports. Good info in a directly usable format.

    Verstuurd door Patrick Bowen, 12/06/2012 4:36pm (8 jaar geleden)

  • This is some essential info here, thank you Balbuss!

    Verstuurd door Sam Jarvis, 10/02/2012 3:56pm (8 jaar geleden)

Het versturen van reacties is uitgeschakeld.

RSS feed voor reacties op deze pagina