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.

Blog Module /

Discuss the Blog Module.

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

Overriding Blog Classes


Go to End


5 Posts   1684 Views

Avatar
Rodolfo

Community Member, 11 Posts

21 March 2012 at 12:10pm

Hi,

I'm trying to override a function in TagCloudWidget using a custom class but SS seems to ignore this new file.

Basically what I did was:

- I created a new class in /mysite/code/CustomTagCloudWidget.php with the following structure

<?php

class CustomTagCloudWidget extends TagCloudWidget {

    function TagsCollection() {
       // new stuff
    }
}

?>

- Added the useCustomClass method to my _config.php file

Object::useCustomClass('TagCloudWidget', 'CustomTagCloudWidget', true);

Am I missing something?

Thank you in advance.

Regards,
Rodolfo

Avatar
Rodolfo

Community Member, 11 Posts

21 March 2012 at 12:52pm

I just tried to override the getCMSFields() method for the BlogHolder class and I'm getting the same behavior.

Is there a special way to override module classes?

Avatar
novaweb

Community Member, 116 Posts

21 March 2012 at 2:58pm

Mr Rodolfo,

I could be wrong, but I have run in to this in the past. It's because only objects instantiated using Object::create can be overridden using Object::useCustomClass

Instead of ::useCustomClass, try ::add_extension

http://doc.silverstripe.org/sapphire/en/reference/dataobjectdecorator

Chur!

Avatar
Rodolfo

Community Member, 11 Posts

22 March 2012 at 8:34am

Hi Nova,

Thanks for the reply.

I gave it a go but it didn't work. After reading the docs more closely I realized that this won't do what I want, which is replace the behavior of an existing method. The use of the decorator pattern allows me to dynamically add methods to an existing object but not override them.

There must be a way to override classes in the Blog module....

Avatar
novaweb

Community Member, 116 Posts

22 March 2012 at 8:45am

Hi Rodolfo,

If you extend the class you wish to extend in Blog, then use add_extension in mysite/_config.php, then... you should be able to redeclare the class you wish to override, rewrite the class to follow your new logic, then the old class should be ignored?

Could be wrong but it may work.