Robin S Posted April 21, 2016 Share Posted April 21, 2016 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 More sharing options...
kongondo Posted April 21, 2016 Share Posted April 21, 2016 One way to do it: Grab a coffee (optional) Copy over the core module to your /site/modules/FieldtypeCommentsCustom/ Rename the files, e.g. FieldtypeComments.module to FieldtypeCommentsCustom.module Rename all PHP classes in that folder to e.g. 'OriginalNameCustom'. For instance: FieldtypeComments could become FieldtypeCommentsCustom Make your changes to the files in FieldtypeCommentsCustom folder Install the module FieldtypeCommentsCustom (could have been done before #5 if no database schema changes made) Create a field of type FieldtypeCommentsCustom, add it to your template, edit a page...the usual drill Take your third sip of the coffee in #1 (optional) 4 Link to comment Share on other sites More sharing options...
Robin S Posted April 21, 2016 Author Share Posted April 21, 2016 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 More sharing options...
kongondo Posted April 21, 2016 Share Posted April 21, 2016 #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 http://stackoverflow.com/questions/2994758/what-is-function-overloading-and-overriding-in-php http://www.techflirt.com/tutorials/oop-in-php/overloading-and-overriding.html https://www.codecademy.com/forum_questions/516763621eb11e53a20018d5 http://www.phpro.org/tutorials/Class-Hierachies-And-Overriding.html http://www.phpzag.com/overloading-and-overriding-in-php/ http://php.net/manual/en/language.oop5.visibility.php http://stackoverflow.com/questions/4361553/php-public-private-protected http://www.techflirt.com/tutorials/oop-in-php/visibility-in-php-classes.html 5 Link to comment Share on other sites More sharing options...
Robin S Posted April 21, 2016 Author Share Posted April 21, 2016 Thanks for all the great info! Lots of learning ahead of me but I love it. Link to comment Share on other sites More sharing options...
kongondo Posted April 21, 2016 Share Posted April 21, 2016 For some methods you might want to just return parent::nameOfMethod() 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now