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?


Reply

5 Posts   331 Views

Avatar
Mmm

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=https://github.com/heyday/silverstripe-menumanager]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.

Avatar
Willr

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 - http://doc.silverstripe.org/framework/en/reference/dataextension

Your extension could look something like

<?php

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

Avatar
Mmm

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?

Avatar
Willr

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.

[1] http://www.php.net/manual/en/language.oop5.overloading.php#object.get
[2] http://api.silverstripe.org/3.1/class-ViewableData.html#___get

Avatar
Mmm

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

That explains it. Thanks a lot!