Follow Examples

One of the best resources for developers approaching Drupal for the first time is going to be the Drupal Examples Project.

Bookmark the API Docs

Although the community documentation has instructions for interacting with the API, the ultimate reference is going to be the code itself, followed by the API documentation, which is generated automatically from the code comments inserted by the Drupal developers. This is especially true for Drupal 8 at the moment, as there is a lag between updates to the code, and updates to the community documentation.

Handy modules

The Devel, Devel_FormInspect, and Coder modules are great tools to help with development work. You can install using

drush dl devel
drush en devel

Drush

Drush stands for Drupal Shell, and gives you a way of interacting with your Drupal installation directly from the command line, without loading your site in a browser. From everyday clearing of caches through to complex management of features, Drush allows you to perform tasks faster than you can using equivalent commands from the administrative interface.

Develop on a Page

If I'm adding something very new to my module, I often develop it first in a PHP page on one of my test sites - it's just easier and potentially less disruptive. I still follow the same development "rules" - only the environment is different. If it doesn't work out, it's much easier, emotionally, to throw away a single page than stuff that's already in a module.

Enable "Development" Mode

In Drupal 8, copy the file from sites/example.settings.local.php to sites/default/settings.local.php. (you may need to unlock write access to the default folder) Then in sites/default/settings.php, uncomment the 3 lines at the end that deal with settings.local.php. (again, you may need to unlock write access to settings.php)

Monitor for Errors

If you run the command tail -f /var/log/apache2/error.log (exact location of your log file may vary) in a terminal window, it turns that window into a real-time monitor of PHP errors that may result from changes to your code. If you want to filter out the parts that are within square brackets, try tail -f /var/log/apache2/error.log | sed -e $'s/\[.*\]//' This even works over ssh, so you can monitor the php errors occurring on your development machine from a second machine where you have the Drupal IRC channel open, etc, by doing ssh -t remote_computer_name "tail -f /var/log/apache2/error.log" | sed -e $'s/\[.*\]//'

Use module filter

To make it quick and easy to find a module (or two), install module filter which allows you to have a search box and grouping on the modules page. Very handy for large sites or sites using a multi site installation.

hook_enable / hook_disable

These hooks are often overlooked but I find them useful:

  1. Set/reset all your variables to their defaults in this function for an easy way back to an almost-virgin state and to make sure the defaults only have to be correct in one place.
  2. Provide a message that will direct your users to the settings page.
  3. Log the userid of the enabling/disabling user.

hook_help

It's always a good idea to help the end-user, so at least start with a skeleton hook_help. It can be filled in more completely as you go along.

hook_menu

It's a rare module that doesn't have at least one menu item, so go ahead and start with a skeleton for this.

hook_uninstall

Even if you didn't create any tables or content types that should be cleaned up, I can pretty much guarantee that you used some variables (i.e. variable_get, variable_set). Delete them. If you created blocks, it's a good idea to clean those up too. Don't forget to test it.

Hook documentation

Drupal has many more hooks than the ones above. You can find documentation about them at https://api.drupal.org/api/drupal.

To do list

Invariably you will think of something that needs to be done sooner or later. Put a small comment section at the top of your module for including these notes.

Comments

juan_g’s picture

Another helpful module is Module Builder, which generates starter modules, an idea similar to starter themes such as Zen, etc.

zaurav’s picture

I recently stumbled upon Drush and now use its cli for enabling disabling modules (to check that the schema specified in .install are getting created/destroyed).

Its so much easier to just type en [mod_name] than to go to administer/modules then scroll down to your module and enable and wait for it.

Offcourse drush can be used for so much more.

Happy coding!

dbolser’s picture

I don't suppose you could provide to links to documentation showing how to use / create the various hooks described here? That would be a really useful addition.

Cheers,

NancyDru’s picture

Added above.