How to not hack core

Submitted by Marc on Wed, 09/22/2010 - 10:19pm

Okay, so you are ready to start customizing your Drupal installation. Before you get started, there are some things you should know and follow.

  • Drupal provides a directory structure that is designed to be used in a certain way. Notice the subdirectories below your Drupal root:
    • includes
    • misc
    • modules
    • profiles
    • scripts
    • sites
    • themes

    Basically anything that you write/add should be placed somewhere below the sites subdirectory. So, when you add a contributed theme, don't place it under the themes directory. Place it somewhere under sites, like sites/all/themes. Contributed modules go under something like sites/all/modules. Get the idea? If you are running a multi-site, single code base, then you will want to place a site-specific theme under sites/ Likewise for modules and other things.

  • Want to change how a core module functions? Drupal provides different API's to make that happen. Learn about the hook structure.
  • Want to change how a theme works? Create a sub-theme that uses the other theme as a base theme. And no, they *don't* have to exist under the same directory any more. You can place your subtheme under its own directory, like sites/all/themes/mysubtheme. By the way, for this theme, I have copied the Andreas03 theme and renamed it. Notice that I still give credit to the originators (Andreas03 is a website template that was ported to Drupal).

This set of conventions is actually for *your* benefit! Drupal can find things in many different places; it doesn't matter to the Drupal CMS. It *does* matter to you. If you make sure to place all of your "stuff" under the sites subdirectory, you will have a much easier time upgrading to the new releases and versions. Since you know that all of your modifications live under the sites subdirectory, you know that you just need to save that subdirectory and apply it to the upgrade version.

By the way, you also don't want to hack contributed code. Well written contributed code will also provide the "hooks" that will allow you to customize it without having to hack inside of it. If you stay out of a contributed module's directory, then you can easily upgrade that module without losing all the custom modifications you made. If you find that you really want to modify existing code, then you should do so with a patch. Or, you can ask to be added as a co-maintainer.