SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Snippets - Little bits of code to make you happy

A Data Object Manager Cheatsheet

We all love the DataObject Manager (DOM, not to be confused with the Document Object Model) as well as its creator UncleCheese of LeftAndMain.com. However one thing which has always frustrated me is having to look back through my code or even worse having to search the forums to find that function to set the upload folder, or create a filter for my DOM. So here within this page, lies a comprehensive list of all the things you can do with your DOM and File/ImageDOM. Viva la DOM!

Regular DataObjectManger

The regular DOM is great for most DataObject related tasks. Here is how you would define it in your getCMSFields() function

//LONG DEFINITION
$manager = new DataObjectManager(
	$this, // Controller
	'MyDataObjects', // Source name
	'MyDataObject', // Source class
	array(
		'Field_1' => 'Field one',
		'Field_2' => 'Field two',
		'Field_3' => 'Field three'
	), // Headings 
	'getCMSFields' // Detail fields (function name or FieldSet object)
	// Filter clause
	// Sort clause
	// Join clause
);
$fields->addFieldToTab("Root.Content.DOM", $manager);

//SHORT DEFINITION - requires function getCMSFields() and 
//static $summary_fields to be defined in MyDataObject 
$manager = new DataObjectManager(
	$this,
	'MyDataObjects',
	'MyDataObject'
);
$fields->addFieldToTab("Root.Content.DOM", $manager);	

//SUPER SHORT DEFINITION - Assumes the first has_many 
// relation is the one to use
$fields->addFieldToTab("Root.Content.DOM", new DataObjectManager($this));

Settings

//The add Title
$manager->setAddTitle('My Title');

//Set the singlar title
$manager->setSingleTitle('Single Object');

//Set Plural Title
$manager->setPluralTitle('My Objects');

//Set the column withs, in % so must total 100
//Note the keys in this array should
//match those in $summary_fields
$manager->setColumnWidths(array(
	'Field1' => 10,
	'Field2' => 60,
	'Field3' => 20
	)
);		

//Set the Width of the popup (in pixels)
$manager->setPopupWidth(400);

//Set the per-page dropdown values
$manager->setPerPageMap(array(20,40,80));	

Permissions

//Turn on or off the Confirm delete popup
$manager->setConfirmDelete(true);

//Add a permission (edit, add, delete, show,upload(FileDOM))
$manager->addPermission('edit');

//Remove a permission (edit, add, delete, show, upload(FileDOM))
$manager->removePermission('add');

//Set permissions
$manager->setPermissions(
	array(
		"show",
		"edit",
		"delete",
		"add"
	)
);

Adding a Dropdown Filter

//Setup the filter for the DOM
$manager->setFilter(
   'ColorValue', //Fieldname
   'Filter by color',//Title
	array('Red' => 'Red', 'Green' => 'Green') //Source options array
);		

//Set the none label
$manager->setFilterEmptyString('--No filter--');

File/ImageDataObjectManager

The FileDOM adds features in to the DOM specifically for files, such as multiple uploads and a Grid view. The ImageDOM takes it even further with scalable thumbnails.

//LONG DEFINITION
$manager = new FileDataObjectManager(
	$this, // Controller
	'MyDataObjects', // Source name
	'MyDataObject', // Source class
	'MyFile', //File relation on DO
	array(
		'Field_1' => 'Field one',
		'Field_2' => 'Field two',
		'Field_3' => 'Field three'
	), // Headings 
	'getCMSFields_forPopup' // Detail fields (function name or FieldSet object)
	// Filter clause
	// Sort clause
	// Join clause
);
$fields->addFieldToTab("Root.Content.DOM", $manager);

//SHORT DEFINITION - requires function getCMSFields() and 
//static $summary_fields to be defined in MyDataObject 
$manager = new FileDataObjectManager(
	$this,
	'MyDataObjects',
	'MyDataObject',
	'MyFile'
);
$fields->addFieldToTab("Root.Content.FileDOM", $manager);	

//SUPER SHORT DEFINITION - Assumes the first has_many
// relation of type 'File' is the one to use
$fields->addFieldToTab("Root.Content.DOM", new FileDataObjectManager($this));	

Settings

//Set the view type on load: grid/list
$manager->setDefaultView('list');

//Set the label on grid view
$manager->setGridLabelField('Name');

//Set the browse button text
$manager->setBrowseButtonText('Choose some files!');

//UPLOADING

//Set the max number of uploads at one time
$manager->setUploadLimit('10');

//Set the allowed file types
$manager->setAllowedFileTypes(array('pdf', 'doc'));

//Remove the folder selection from uploadify
$manager->allowUploadFolderSelection = false;

//Set the upload folder
$manager->setUploadFolder('Uploads/my-files');

General DOM Stuff

Here are a few bits and bobs which relate to using the DOM.

Making Objects Sortable

//Single
SortableDataObject::add_sortable_class('MyObject');

//Multiple
SortableDataObject::add_sortable_classes(array(
	'MyObject',
	'AnotherObject',
	'YetAnotherObject'
));		

//many_many sortable objects, with arg1 being the ParentClass and arg2 the Child relation
SortableDataObject::add_sortable_many_many_relation("Page", "MyDataObjects");

Limiting the Number of Items 

Sometimes you may want to only have a limited number of items attached to a page through the DOM. This can be acieved with this handy if() statement, which when added to your DOM definition will check how many items are attached and remove the 'add' permission accordingly. Best thing is it works dynamically so even if the user is adding one item after another as soon as it get's to 6, the add item button dissapears!

//LIMITED TO 6 ITEMS
if($this->MyObjects()->Count() > 6){
	$manager->removePermission('add');
}	

Using the SortHand Definition

In order to use the shorthand definition shown above, you need to ensure you have 2 things in your DataObject class. 1. A $summary_fields static and 2. You are using getMCSFields() as your popup fields function. So your DataObject class might look like this:

class MyObject extends DataObject{
	.
	.
	.
	
	//Summar fields
	static $summary_fields = array(
		'Field_1' => 'Field one',
		'Field_2' => 'Field two',
		'Field_3' => 'Field three'
	);
	
	//CMS fields
	function getCMSFields()
	{
		$fields = new FieldSet();
		
		$fields->push(new TextField('Field_1', 'Field one'));
		$fields->push(new TextField('Field_2', 'Field two'));
		$fields->push(new TextField('Field_3', 'Field three'));
		
		return $fields;
	}
	
	.
	.
	.
}

Add CSS or JavaScript Requirements to the Popup

Sometimes you may want to customise the form within a DOM, using CSS or JS. To do this you need to include your custom files within the popup window, which is not possible when using the sandard DOM class. So in order to to it, you just have to create your own class which extends DataObjectManager (or FileDataObjectManger) and calls it's own Popup class which includes your custom requirements. You will need to add the following code, either at the bottom of your Page.php file or in it's own file called MyDataObjectManager.php:

class MyDataObjectManager extends DataObjectManager {
	public $popupClass = "MyDataObjectManager_Popup";
}
//Custom Popup Class
class MyDataObjectManager_Popup extends DataObjectManager_Popup
{
    function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) 
    {
    	parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
       
	    //Custom JS
        Requirements::javascript('mysite/javascript/jquery.mydom.js');
        //Custom CSS
        Requirements::CSS('themes/MyTheme/css/mydom.css');
    }
}  

 

Then when you define your DOM in getCMSFields() make sure you use your custom DOM class: MyDataObjectManger() like so:

$manager = new MyDataObjectManager(
	$this,
	'MyObjects',
	'MyObject'
);	
$fields->addFieldToTab("Root.Content.MyDOM", $manager);	

So that's it! As always if I have forgotten anything you think should be in here, just comment and I will add it in :)

Aram Balakjian avatar

Aram Balakjian

Aram is a web developer running London based agency Aab Web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.

  • joel
    06/10/2010 2:21pm (4 years ago)

    Hi Aram, great stuff here. Anyway, the last part is interessting. What kind of stuff could you do with javascript inside a DataObjectManager?

  • Andrew Houle
    06/10/2010 2:29pm (4 years ago)

    This is great Aram. I had been adding coda snippets for most of these as I've gone along. It's nice to have this handy reference for the ones I've missed. Thanks!

  • Aram Balakjian
    06/10/2010 3:17pm (4 years ago)

    @Joel - I normally use it to show/hide fields based on a dropdown selection, for example if I have a link DO with various types of link (internal external etc.) then I will only show the relevant fields for the selected type

  • joel
    08/10/2010 12:55pm (4 years ago)

    @Aram – Aha, cool, good idea, this could be very useful..

  • Samuel
    09/10/2010 8:24am (4 years ago)

    Hi Aram,

    In your code:

    //Add a permission (edit, add, delete, show,upload(FileDOM))
    $manager->removePermission('edit');

    Shouldn't we use $manager->addPermission()?
    Is that a typo, or I misunderstood what you means?

  • Aram Balakjian
    09/10/2010 7:58pm (4 years ago)

    @ Samuel - Great catch, thanks!

  • alex li
    20/10/2010 12:03am (4 years ago)

    Looks pretty cool.. and it was useful!!!

  • Andrew Houle
    25/10/2010 8:38pm (4 years ago)

    I tried to add a required css to the DOM popup by extending DOM like you show at the end of this cheatsheet, but it's not taking for some reason. I'm not getting an error, and the css now shows MyDataObjectManager so I know it's somewhat working, but when I check the iframe source, there is no evidence of the new css requirements. Any idea, what I could be missing? Or what I could check?

  • Andrew Houle
    25/10/2010 9:57pm (4 years ago)

    I figured out it was the order of the code. It needs to be like this...

    function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
    parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
    Requirements::css('yourcss');

    }

  • Don St. Troy
    21/01/2011 3:36am (4 years ago)

    I have not figured out how to do a Sort clause, or Join clause. I viewed on the forum that if you need to do a sort, you need to have a join as well.

  • moloko_man
    11/02/2011 5:12pm (4 years ago)

    very useful cheatsheet, Thanks!!

  • Manuel
    14/05/2011 1:02pm (4 years ago)

    I have a small extension for MyDataObjectManager_Popup:

    class MyDataObjectManager_Popup extends DataObjectManager_Popup
    {
    function __construct($controller, $name, $fields, $validator, $readonly, $dataObject)
    {
    parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);

    //Custom JS
    Requirements::javascript('mysite/javascript/jquery.mydom.js');
    //Custom CSS
    Requirements::CSS('themes/MyTheme/css/mydom.css');


    // dynamically add requirements from within the managed DataObject
    if ($dataObject->hasMethod('getRequirements_forPopup')) {
    $dataObject->getRequirements_forPopup();
    }

    // add a validator to validate the popup form
    if ($dataObject->hasMethod('getCMSValidator')) {
    $validator = $dataObject->getCMSValidator();
    $validator->setForm($this);
    $this->validator = $validator;
    }
    }
    }

    With hat you can add Requirements to the DOM popup from within the DataObject (e.g. JS to hide/show fields) and you can add a form validator, if one is defined in the DataObject.

    Hope it helps.

  • pinkp
    30/05/2011 9:16am (3 years ago)

    Do you have an example / tutorial of how to use JS to show/hide the relevant fields in the pop up?

    I have a check box in my gallery pop up which if checked I would like the following fields to hide as they are no longer relevant.

    thanks

  • Mom Bunheng
    10/07/2011 10:34am (3 years ago)

    Hi Aram,
    Is there any tutorial relating to custom filter within dataojbect, especially filter on related dataobject like in Image gallery module made by UncleCheese.

    Thanks

  • k_k
    16/03/2012 9:31pm (3 years ago)

    hi aram! great work , i'm wondering if there's a way to activate the auto-refresh in a dataObjectManager, or a button or something, rigth now i'm make it by myself...as i can...i have lost 3 nails and a thoot XD!
    thanks in advance!

  • ayyurek
    26/04/2012 9:11pm (3 years ago)

    Hi Aram,

    In the Regular DataObjectManager section, LONG DEFINITION code has a mistake.
    line 11 should be getCMSFields_forPopup not getCMSFields
    :)

  • Aram Balakjian
    27/04/2012 11:03am (3 years ago)

    Hi Ayyurek,

    That can actually be whatever you want it to, it just defines what function the DOM should call to grab the edit fields. It defaults to getCMSFields, but could use getCMSFields_forPopup if you wanted different fields for the popup and another section you were editing that object in, for example ModelAdmin.

    Aram

  • r0nn1ef
    20/06/2012 2:04am (2 years ago)

    Does anyone know if it's possible to add a "Delete Selected" button and checkboxes to DataObjectManager's display list? I have a site that yearly needs to update team members and removing one-by-one isn't very user friendly. Any help would be appreciated.

  • Timothy
    29/05/2013 6:56am (1 year ago)

    Hi I was wondering if anyone could help, I'm getting an error "undefined index: search" here is my code,

    $fields->addFieldToTab('Root.Content.Organisations', $manager = new ManyManyDataObjectManager($this, 'Organisations', 'Organisation', array('Title' => 'Title'), null, null, '"SiteTree"."Title" ASC'));

    Thanks in advance!
    Tim

  • neerom
    02/06/2013 3:12am (1 year ago)

    Please help.... DOM pop up opens on the same page.

    $f = parent::getCMSFields();
    $manager = new ImageDataObjectManager(
    $this, // Controller
    'Images', // Source name
    'ImageResource', // Source class
    'Attachment', // File name on DataObject
    array(
    'Title' => 'Title',
    'Caption' => 'Caption'
    ),
    'getCMSFields_forPopup'

    );
    $f->addFieldToTab("Root.Content.Gallery",$manager);

    return $f;

  • ilovemoon
    09/07/2013 4:09pm (1 year ago)

    Hi Aram,
    Some great stuff here!
    Is it possible to limit the amount of characters/words in the $summary_fields?
    I have two fields which I would like to display but maybe only the first sentence of the second field which can contain a lot of text.

  • Aram Balakjian
    09/07/2013 4:33pm (1 year ago)

    Hi ilovemoon,

    This is pretty straightforward, just use the same principle as this: http://www.ssbits.com/snippets/2009/adding-a-thumbnail-to-a-dataobjectmanager-or-complex-table-field/

    Instead of returning the image, just return a limited carachter string, using $this->dbObject('TextField')->LimitWordCount(20);

    Aram

  • Blair
    06/12/2013 12:28am (12 months ago)

    Hi Aram,

    This article has been extremely helpful while getting my head around creating a ManyManyDOM - Thank you!

    Unfortunately i'm having an issue with the sortable code for a many to many relationship.

    Putting the following recommended code into my sites _config.php file, unfortunately throws a large query error:

    Code in _config.php:
    "SortableDataObject::add_sortable_many_many_relation("ProductCategory", "Products");"

    Error:
    [User Error] Couldn't run query: SELECT "SiteTree"."ClassName", "SiteTree"......etc etc
    -I can post the full error for you if you like, it's pretty large though...

    This is for the same site as my other problem on creating the thumbnail for the ManyManyDOM - working to integrate the code into SS 2.4.7 and Shop 0.8.5

    Would definitely appreciate any help on this - thank you!

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image Ty Barho
6 article image Martijn van Nieuwenhoven
7 article image Darren-Lee
8 article image Roman Schmid
9 article image Matt Clegg
10 article image dalesaurus

View full leaderboard


Advertisement