Creating a custom Drupal module is a lot easier than you expect, and a lot more important as well.

Drupal offers thousands of resources for you to create your website in a glance, I know, but I am pretty sure you or your client will want to change a little something here and there. This is why I recommend you to have a custom module for every website you create with Drupal. Within it you will create those PHP snippets you usually need and would end up placing within blocks, nodes or rules wasn't for the custom module.

Tip: try always to keep adherent to the good practices and patterns of Drupal community. This includes avoiding PHP in blocks or nodes. Deadlines may be an obstacle for understanding and learning the patterns, but if you cannot find some time to do it you will end up with more problems in the future.
In order to create a "blank" module, ready for use when you need a custom function, it will be necessary to create a folder and two files: piece of cake. After that you will need to go to the module admin page and install you newly created module.
  1. Access the folder in which you usually place your modules: i.e.: /sites/all/modules or /sites/default/modules);
  2. Create a folder named after your new module:
  3. mkdir mymodule
  4. Access this folder and create two files, both named after your module, one with the ".info" extension and other with the ".module" extension.
  5. touch mymodule.info
  6. touch mymodule.module
  7. Open the file mymodule.info and write the following information:
  8. ;$Id$
    name = mymodule
    description = this site specific custom module.
    core = 7.x
    
  9. Remember to change the information above, including the core version to your Drupal installation core.
  10. Now open the mymodule.module file and write the functions you want. They will be available in your snippets called from blocks, of course, but the best way to develop with Drupal is to use hooks. After learning how to use hooks Drupal may become not only your favorite CMS but also your favorite framework.
  11. IMPORTANT: use <?php to open the PHP code, but do not use ?> to close it in the .module file
comments powered by Disqus