Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

All other Modules

Discuss all other Modules here.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

How to extend a user created module?


5 Posts   331 Views


29 January 2014 at 3:18am Community Member, 3 Posts

Hi all.

First of all, if this is wrong subforum, please move it to correct one. I was unsure where to post it.

This is a specific-case question, but as well a general one. I am new to Silverstripe, coming from being Wordpress developer, using the latest release. I need to create custom menu set, for which I have choosen [url=]Heyday Menu Manager[/url]

This works ok: I can define custom menu sets, menu items that might not be linked to Pages inside SS installation. All great. Unfortunately, I need a class for each generated item in the loop, which can be based on the MenuTitle. Alas, MenuTitle can contain spaces, so I need to get rid of those before.

My first aim was to modify the MenuItem.php code inside the extension itself, by adding this inside the __get method:

      if( $field == 'MenuClass' ) {
         return str_replace("\s", '', $this->__get('MenuTitle'));

This does work, I get an additional MenuClass property to use inside the loop. Unfortunately, this is not the way I'd like to tackle the issue, since any further update to the extension itself won't be available due to modified base code.

I can just fork the project on Github, add MenuClass (or MenuLabel) inside fields of MenuItem and just work that way, but still this is prone to too much problems in my opinion.

Therefore, I'd like to learn how to add extensions to custom classes, especially ones that call their own children classes inside (MenuManager's MenuSet calls MenuItem internally). Can someone suggest me a way to solve this? I am a little bit overwhelmed by the "magic" that happens behind the scenes in Silverstripe.


29 January 2014 at 8:23pm Forum Moderator, 5511 Posts

In this case MenuItem is a DataObject so to add functionality you can use the DataExtension class -

Your extension could look something like


class MenuItemExtension extends DataExtension {

public function getMenuClass() {
return str_replace("\s", '', $this->owner->MenuTitle);

Add the extension to the MenuItem class and you should then be able to use $MenuClass or $item->getMenuClass() / $item->MenuClass


29 January 2014 at 11:15pm Community Member, 3 Posts

Thank you a lot, this has worked.

I do have a question: how does Silverstripe parse it? Does it look the class + extensions for the get<VariableName> method?


30 January 2014 at 8:04pm Forum Moderator, 5511 Posts

Yes. SilverStripe overrides the __get() [1] [2] method to provide 'nice' ways like that. Prefixing it with get is not required though it can make for a more consistent API.



30 January 2014 at 9:08pm Community Member, 3 Posts

That explains it. Thanks a lot!