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.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

All other Modules /

Discuss all other Modules here.

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

How to extend a user created module?


Go to End


5 Posts   956 Views

Avatar
Mmm

Community Member, 3 Posts

29 January 2014 at 3:18am

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 Heyday Menu Manager

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

Forum Moderator, 5523 Posts

29 January 2014 at 8:23pm

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

Community Member, 3 Posts

29 January 2014 at 11:15pm

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

Forum Moderator, 5523 Posts

30 January 2014 at 8:04pm

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

Community Member, 3 Posts

30 January 2014 at 9:08pm

That explains it. Thanks a lot!