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.

Customising the CMS /

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

Inheritance and Classes


Go to End


3 Posts   940 Views

Avatar
Dorsai

Community Member, 12 Posts

15 April 2015 at 11:17am

Hi,

I have a module that creates a Table of Contents for a page based on the h1 through h6 tags in the content. I want to alter the function in the module that steps through the content so it only looks for h2 tags. Now I can make this work the way I wish by directly editing the function in the module but I am aware this is very bad practice.

Instead I am trying to use inheritance to overide the module function by creating a file in mysite/code with a class that extends the module function, which contains a copy of the function code modified to only look for h2 tags.

This however does not work, on the upside it is not breaking the site, and I am wondering where I am going wrong with this? If anyone has any pointers on this it would be greatly appreciated.

Cheers
H.

Avatar
helenclarko

Community Member, 166 Posts

15 April 2015 at 1:42pm

Hi Dorsai,

If you are extending a class you will need to declare it in either a .yml or your _config.php file.

For example, if you are extending DataExtention like you would if you were adding to site config:

class CustomSiteConfig  extends DataExtension {

You would need to do the following in your _config.php:

SiteConfig::add_extension('CustomSiteConfig');

OR

In a .yml file:

SiteConfig:
  extensions: 
    - 'CustomSiteConfig'

Hope that helps to solve the problem.

-helenclarko

Avatar
Dorsai

Community Member, 12 Posts

15 April 2015 at 3:26pm

Hey thanks for that,

Still not quite across the finish line though....

In a bit more depth, the module has this:

class Tocifier {
...
  private function _processDocument($doc) {
    ... code ...
  }
...
}

in mysite/code I have a file with the following:

class MyAutoToc extends DataExtension {
 private function _processDocument($doc){
   ... altered code ...
 }
}

and finally in /mysite/_config.php I have the line

Object::add_extension('Tocifier', 'MyAutoToc');

So while everything builds without error, the altered code in my function is apparentyl not superseding the original code. I feel I am missing something very basic here.

Cheers
H.