5067 comments
1897 subscribers
4798 on Twitter
Subscribe! Feed reader E-mail

Drupal: Changing module behavior without changing the source code

For the project I’m currently working on, I’m not allowed to make any changes to third-party source code. Yes, I’ve pointed them to the GPL and Drupal FAQs about being legally allowed to make modifications to a system that will be hosted but not publicly discributed, without being required to contribute those changes back to the community. As much as I would like to release all the modifications back to the community, I also care about proper interpretation of the GPL. I suspect our project restrictions are a combination of wanting to be able to upgrade modules easily and erring on the safe side when it comes to the legal requirements of open source.

Anyway. If you’re trying to avoid making changes to third-party source code when you’re customizing your Drupal system, here are several ways you can alter the module’s behavior:

  • Hooks: Drupal has an extensive hook system allowing you to alter forms (hook_form_alter), node behavior (hook_nodeapi), user behavior (hook_user), and do other cool things. Read about the hooks in your version of Drupal, and see if any of those hooks can help you wrap code around what you want to modify.
  • Theme functions: Well-written Drupal modules use theme functions to make the HTML output easily customizable. Check the function you want Create a custom theme and override the functions in your template.php. For example, if you’re using the PHPTemplate engine, you can override a theme_foo_bar(…) method by defining a phptemplate_foo_bar(…) function in your template.php.
  • String overrides: If you want to change a displayed string and it’s wrapped in t(‘…’) calls (as it should be), use String Overrides to set up an English-to-English translation for that string. If you use the internationalization and localization features of Drupal, you don’t need String Overrides – just set up custom English translation for your site.
  • Menu overrides: Use this if the module outputs HTML directly, or if you want to change more of the functionality. In your custom module, you can define a menu item with the same path as a menu item defined by a different module. Copy the menu item definition from the other module, then change the callback function and the arguments to point to customized copies of the functions. If you do this, check the {system} table to make sure that your module has a greater weight than the module it’s overriding. You may have to duplicate a number of functions in order to get to the menu item you need to change.
  • String replacement: If you’re desperate, you can use string replacement on the page before it’s sent to the browser. Use this only in emergencies, as it’s ugly and it wastes all the processing time spent coming up with the string you’re going to replace.
  • Module replacement: If you’re absolutely desperate, make a copy of the module and change all the names so that you remember this is a customized module instead of something that you can cleanly update.

Your Drupal modifications are also under the GPL, so pay attention to the licensing terms. =)

Short URL: http://sachachua.com/blog/p/5424

5 Responses to “Drupal: Changing module behavior without changing the source code”

  1. Here’s a confusing bit – the module with the lower weight in the {system} table gets priority in terms of label, but the model with the higher weight appears to get priority for execution. Boggle!

    I’ll update this when I figure out the proper way to override that.

  2. Nice write up. I enjoy your writings every time :-)

    About Menu Overides: In drupal 6 (or 7) you would use hook_menu_alter();

  3. It is a good policy – hacks to contrib modules, unless it is a relatively obscure and infrequently updated module, are only slightly less discomforting than hacks to core. Upgrades become difficult, changes must be continually ported, and even with source control there is a time cost with tracking the individual changes.

    I always have a sitename_custom.module, and do what you described above: leverage the various hooks and such in order to change functionality. In those rare cases where a module hack is absolutely required, I document the change externally (usually in a project tracker), make it a discrete commit to a subversion repository, put a clear identifier in the source code comments so that I can easily ‘grep’ for all custom changes, and – when applicable – submit a patch to the module maintainer in the hopes that the change or fix will be officially committed so that I won’t have to maintain my own fork going forward.

  4. hmmm…
    that weights thing is sure a problem

    I think it affects files I include via module_load_include() too.

    It does include it before its needed, But the function it defines returns false for function_exists()!

    or am a a total dunderhead to miss something and blame it unnecessarily on weights

  5. it so happened that all our modules had a default weight of 0
    We had a ‘news’ module, a ‘nodewords’ module and a ‘research’ module

    We needed ‘nodewords’ to work for both research and news. A simple inc file with the required functions worked for news while the same didn’t for research. This was because news_init() was called before nodewords_init() and only then was research_init() called. So we made nodewords put on some weight.

    And I didn’t get your “priority in terms of label” statement :-/

Comment, share a thought, ask a question...

Please comment as you, not your organization.





 

On This Day...

  • 2011: Quantified Awesome: Thinking about what I want to build next — One of the best ways to make the most of those little pockets of time is to keep a someday [...]
  • 2010: Happy Holidays, Eh! First edition greeting card giveaway — Skip my VistaPrint review and go straight to the giveaway Last week, I thought: hey, what would a Canadian Christmas [...]
  • 2009: Notes from VizThink video on Visual Notetaking 101 — Click on the image to view a larger version. Next actions: Post notes Practice with podcasts, webcasts, books, and teleconferences [...]
  • 2008: Drupal and return on sharing — Another IBMer sent me an instant message out of the blue and introduced himself as a fellow Drupal developer. I [...]
  • 2008: Twitter, Whuffie, and Amazing Connections — Thanks to Don Marti’s post about a possible business model for Twitter, I came across Tara Hunt’s post about Twitter, [...]
  • 2007: The evils of blur — I left my purse at the Bay food court at around 1:30 this afternoon. When I realized this at 5:00, I [...]
  • 2007: Story: Connecting through social computing — Let me tell you about a recent example of how social computing can help us form better relationships with our [...]
  • 2006: 50 e-mail messages today — Okay, my fingers are tired. No more e-mail. =) Someday I’m going to have to learn how to scale, but today [...]
  • 2006: Inbox meter — I should have one of those almost-real-time inbox meters that count the number of messages I’ve promised to respond to. That [...]
  • 2006: Found my vertical: HR — I ran into Pete Forde at Andrew Burke‘s birthday party last night. He explained to a number of Andrew’s friends in other [...]
  • 2006: Personal contact relationship management — It’s a good thing that computer geeks appreciate automation. They can sniff out form e-mail in seconds, but they don’t mind [...]
  • 2006: Programming competitions — I always love reading Didith Rodrigo’s blog posts about programming competitions and my alma mater, Ateneo de Manila University. Her [...]
  • 2005: Scrabble — In between review sessions for my final exam tomorrow, I managed to play Scrabble with Steve (Biology) and Mike (Math). Much [...]
  • 2004: On the Map — http://www.onthemap.com.ph : street-level Flash-based map site for the Philippines. Nifty. Thanks to Dominique for the link!
  • 2004: Novell Linux-Certified Professional — Dominique‘s the first Novell Linux-Certified Professional in the Philippines. He doesn’t want to gloat on his blog. I, however, can gloat all [...]
  • 2003: Game development in Japan — Hee Soo Lee’s thinking of going for game development in Japan. Way cool. =) E-Mail from Soo Lee
  • 2003: Software Elegance — Gerald Generoso says You might wanna check this out —>> http://www.eskimo.com/~scs/readings/software_elegance.html E-Mail from apache
  • 2003: Printer works again — Apparently, they made it a printer server. Yay!