Jump to:

1851 Posts in 1606 Topics by 561 members

Blog Module

SilverStripe Forums » Blog Module » Overriding Blog Classes

Discuss the Blog Module.

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

Page: 1
Go to End
Author Topic: 554 Views
  • Rodolfo
    Avatar
    Community Member
    11 Posts

    Overriding Blog Classes Link to this post

    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

  • Rodolfo
    Avatar
    Community Member
    11 Posts

    Re: Overriding Blog Classes Link to this post

    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?

  • novaweb
    Avatar
    Community Member
    115 Posts

    Re: Overriding Blog Classes Link to this post

    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!

  • Rodolfo
    Avatar
    Community Member
    11 Posts

    Re: Overriding Blog Classes Link to this post

    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....

  • novaweb
    Avatar
    Community Member
    115 Posts

    Re: Overriding Blog Classes Link to this post

    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.

    554 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.