Now Playing Tracks

Inheritance DataObjectManager

A couple of times on Silverstripe builds I’ve run into the issue of wanting a DataObjectManager to be able to handle sub-classes. For example, a page has tips on it. There are tips which are a table of data, a tip which just has an image, a tip which is editable, a tip which isn’t. In the past I’ve put all the properties I’ve needed onto one tip object plus a type enum.

This means in the DataObject popup you only show type until the DataObject isInDB(). Once it’s in the database then you check the type and show only the fields appropriate for that type.

It works, but it feels nasty. 

It isn’t an issue if you have distinct objects which belong to a page but have no relationship with each other, however if you want a bunch of different things to be sortable then you have to do something as above.

What I’d like to have is one object which handles the relationship and then subclasses which inherit this and only have the properties which they need.

Until now DataObjectManager has been able to have two classes in two different manages but not two (or more) subclasses in the same and it seems a few people would like it.

Turns out it’s not too hard.

InheritanceDataObjectManager class

InheritanceDataObjectMananger template

How to add a InheritanceDataObjectManager to a page in the CMS:

And then the class structure should look like something like this:

You can extend the MyWidget class as many times as you like the InheritanceDOM will pick up the sub classes automatically (after you’ve done a dev/build of course) and add a ‘Add SubclassName’ button above the DataObjectManager.


blog comments powered by Disqus

1 note

  1. experience-ss reblogged this from pitchandtone
  2. pitchandtone posted this
We make Tumblr themes