One way to override Drupal core modules - safely

Every where you read on Drupal.org, Professional Drupal Development book, IRC ect, all of us who know Drupal intimately say "Not to hack core" files. I include core module files with that statement. Here are a couple of reasons why:

  • there should be no reason to hack it. Almost all output from core files can be overriden with the use of theme functions within tempate.php. By changing the output from the theme function you can bypass hacking core files
  • when it comes time to update, chances are you are going to forget about the small changes you made to that one core module file and overwrite those changes. That can cause some headaches if no backups are available.

But what do you do if there are no theme functions to override within a certain core module file? You want to play it safe and not risk writing over your hack(s). For example, the blog.module file, it has no theme overrides in Drupal 5. I have a client who wants to rid the "Post new blog entry" link when visiting your own blog page. Sure you could hide it in CSS but that is dirty. You could do a PHP str_replace() too. Even more ugly.

Here is what you can do. Create a "custom" directory inside the core "modules" directory. Then move the core module you want to hack (it's folder with module files) to the "custom" directory. Then do your hacking. Be sure to visit admin/build/modules, scroll down and click on submit - to reset the module path in the system table. Now when you do an upgrade you won't write over your hacks.

Comments

It's a bit unclear to me. After moving the core module to /modules/custom/ , should I change the code in core module file?? I mean manipulating the ****.module file directly? Please clarify.

<p>Hello ipsita, no you don't have to. Did you run into problems? If so share what version of Drupal you are working with. Also, be sure to reload the site.com/admin/build/modules url, as it will pickup the new path to modules/custom/modulename</p>

<p>Another way to do this is through&nbsp;<a href="http://www.bryght.com/blog/boris-mann/cleanly-overriding-core-modules-in-drupal-through-the-magic-of-multisite">multisite method</a>. I prefer my method simply because it keeps the custom core modules in a the core modules directory.</p><p>The alternative, to use the multisite method and put the core modules inside sites/all/modules/custom_core/coreModuleName</p><p>The effort here is to keep core modules organized and seperate. Thanks to Boris for sharing the <a href="http://www.bryght.com/blog/boris-mann/cleanly-overriding-core-modules-in-drupal-through-the-magic-of-multisite">multisite method</a>.</p>

Why wouldn't you put your hacked modules in sites/all/modules to conform with Drupal coding standards?

<p>@Rob, you are conforming to Drupal standards. Why would you hack a module then update it later and have to your hacked overwritten, or to have to pull your hack into the new updates?</p> <p>If you create your own module and override other modules, which can be done for the most part, you don't have to worry so much about updates breaking your site.</p> <p>Modules go in:<br />sites/all/modules<br /><br />Custom modules and overrides can go in:<br />sites/all/modules/custom</p> <p>In my opinion this is really about good practices, being organized and efficient.</p>

@elvis Please pardon my informality. I didn't literally mean hack the core modules. I was referring to the custom module described in the article. I was just think that instead of putting the module in the core module directory where it cold possibly be overwritten or deleted that it should go in sites/all/modules with the rest of the contributed modules.

<p>After rereading the article, I may have misunderstood the point anyways.</p>

<p>I've tried to move my hacked core module to /modules/custom/myhackedmodule but the module isn't changging at all.. Actually i only add some text in the template.php, but it still remain the same as the default core module. I've also tried to reinstall that module, no error but nothing's changing.. help me pls..</p>

<p>Thanks for your article. Let me see if I get this right: -1- create a folder "custom" in folder "/modules"; -2- move (not copy) the module's folder you want to hack to folder "/modules/custom/"; -3- hack your custom module in folder "/modules/custom/"; -4- browse to folder "/admin/build/modules" and click submit to reset the module path in the system table; Is that right, Elvis? Thanks, Eric</p>

<p>I have been bashing my head against the wall about this too. I had an idea. Comment out original function like so: /* function foo() { // code } */ Then copy it to template.php then make your changes So you always have original and your version. When it comes to time to update, the original function will become uncommented and you will get errors saying "you cant have multiple functions of the same name". So go back in and comment out all the functions again. You can also copy the directory path from where the function was copied and add it as a comments in template.php file so you know where to go in future. The problem i have with all these methods of function overriding is what happens when you update? Your custom module remains old and may be missing some vital new code? What if you have modified over 100 functions? I think ALL functions should have been overrideable from the start.</p>

thanks for the tip, i think maybe by using this method you also need to hack the core files that uses the moved core module and generates path to the module manualy, such as some methods in menu.inc etc.

I am getting white screen error after inclusion of some modules ....i am unable to solve this problem ......please help

<p>Hi, I want to override view's query with my custom query. For that I had made a function say 'views_views_pre_execute(&amp;$view)' and placed that function into sites/all/modules/views.module file. It is working fine. But this is not a correct method with drupal as I am updating a core module file. So how can I override sites/all/modules/views.module? Where can I place my function to override sites/all/modules/views.module? I have placed this function into templates.php file but it is not working....Please let me know...Thanks.</p>

Add new comment