Jump to content

Best way to modify core module


Robin S
 Share

Recommended Posts

What is the best way to make changes to a core module?

My current need is to modify/replace the render functions in FieldtypeComments: render(), renderList(), renderItem(), etc. I know I can iterate over comment items in my template but in this case I'd prefer to use modified functions in the module.

I've already copied FieldtypeComments to my site modules folder and could make the modifications there but I'm wondering if there is a better way. I'd like to make it as easy as possible to upgrade FieldtypeComments if a new version is released. So ideally I don't want to make any changes directly in the core module files. Is there a way I can keep my modified functions in a separate file and use them in place of the core module functions? I thought this might be possible with a hook but I don't see a suitable hookable method.

Link to comment
Share on other sites

One way to do it:

  1. Grab a coffee (optional)
  2. Copy over the core module to your /site/modules/FieldtypeCommentsCustom/
  3. Rename the files, e.g. FieldtypeComments.module to FieldtypeCommentsCustom.module
  4. Rename all PHP classes in that folder to e.g. 'OriginalNameCustom'. For instance: FieldtypeComments could become FieldtypeCommentsCustom
  5. Make your changes to the files in FieldtypeCommentsCustom folder
  6. Install the module FieldtypeCommentsCustom (could have been done before #5 if no database schema changes made)
  7. Create a field of type FieldtypeCommentsCustom, add it to your template, edit a page...the usual drill
  8. Take your third sip of the coffee in #1 (optional)
  • Like 4
Link to comment
Share on other sites

Thanks for the suggestion kongondo. But I think that would effectively be the same as making my modifications to a duplicated Comments module in the site modules folder. The problem being that if I want to later upgrade to a newer version of the Comments module I would need to repeat my code modifications.

Bear with me because I don't really know what I'm talking about here: I think extending the Comments module might be what I (vaguely) have in mind. If I create a module that extends the Comments module does that mean that if my module contains a renderItem() method it would overwrite the renderItem() method in the core module? And I have access to all the core Comments methods within my module?

Link to comment
Share on other sites

#1 Yes...that is what this would entail. This approach is for those who either have a temporary need (e.g. until a PR is accepted) OR for those who've bought a one-way ticket; no looking back :-). Going by what you are saying, this is not for you

#2 Yes, what you are describing is called overriding in PHP (and many other OOP languages). You would have access to protected and public methods and properties but not private ones. Overriding occurs many times in Fieldtypes. Take for example the method ___getConfigInputfields() that's present in quite a number of Fieldtypes. FieldtypeComments has that method. But that's not its origin. FieldtypeComments extends the class FieldtypeMulti, so it inherits it through this class. But wait a minute. FieldtypeMulti is not the origin of that method. Instead, FieldtypeMulti also inherited the method from its parent class, Fieldtype. Look the table here 'Methods inherited from Fieldtype' (you can follow the links deep into the recesses of PW ;-))

Here's some links to helpful resources about this (and similar/related) OOP concepts

  1. http://stackoverflow.com/questions/2994758/what-is-function-overloading-and-overriding-in-php
  2. http://www.techflirt.com/tutorials/oop-in-php/overloading-and-overriding.html
  3. https://www.codecademy.com/forum_questions/516763621eb11e53a20018d5
  4. http://www.phpro.org/tutorials/Class-Hierachies-And-Overriding.html
  5. http://www.phpzag.com/overloading-and-overriding-in-php/
  6. http://php.net/manual/en/language.oop5.visibility.php
  7. http://stackoverflow.com/questions/4361553/php-public-private-protected
  8. http://www.techflirt.com/tutorials/oop-in-php/visibility-in-php-classes.html
  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...