Posted 04.21.2010 by Jack
What is a module?
- allows you to extend Drupal without touching Drupal core code
- wraps related code into packages that can be used in other projects or shared with the entire community
- allows collaboration with the community with common standards
Different types of modules
- Glue modules: put little changes that you need to make to the way that Drupal is processed - not things shared on d.o, but created to achieve specific small things on particular site(s)
- Input filters: take input from users and convert them into something more fantastic (e.g. PHP Filter, Markdown)
- Module plugins: extend established modules that have their own APIs (e.g. Filefield, Views Custom Field)
- Utilities: provide tools for other modules (e.g. token)
- Navigation (e.g. admin menu, teleport)
- Administration (e.g. Admin Menu, Dashboard)
What to ask yourself before you start coding one
- Why am I doing this? Is it really needed? Are you just scared of looking for other modules, installing them, and still not getting what you want?
- Reasons to conquer fear-driven module creation:
- Time is important
- Forks suck - can't split resources, dev time, user base with multiple solutions
- Your reputation is at stake - people will call you on duplicating existing modules
- Reduce risk progressively. You can't know everything about every project out there, but every time you think you need custom code, do this first:
- Do a Google site search of Drupal.org to try to find something that does what you want to do
- Start talking to real people on Drupal User Groups, groups.drupal.org, and IRC to see if they know of an existing solution.
- Install a few modules and play with them, exhaust what they can do so you can learn more and more about existent tools
- Grep an entire contrib repository for what you might need
A few more things before you start...
- Assume your code will be shared and follow Drupal coding standards
- Negotiate the time you're going to put into this - make sure you have enough time to make it look good!
Methods for starting
- Gut (copy and empty) another module
- Frankenstein (copy and paste from other modules)
- Cheat (use templates)
- Ninja (compose from scratch/memory)
Let's pick a name
- Check out contrib from CVS and scroll to make sure your name doesn't exist
- Or do a Google search of Drupal.org
- You're going to be using the module name in all of your functions, so choose your name accordingly (not too long, not too funky)
- Using module name in core files, so keep it short
- Use all lower case letters and underscores rather than dashes
Start building
- Put your module in /sites/all/modules
- create .info file in your folder; your module will now show up in admin/build/modules
- create .module file in your folder; when you enable your module, code in there will execute
- Add other files to your module: JS, CSS, and module broken down into .inc files if necessary
How do modules add or extend functionality?
- Through hooks, which hook in processes
- You don't need to start from scratch because of hooks
- Example Goal: redirect user to appropriate taxonomy term page after submitting a recipe. To do this we'll use hook_form_alter(), use the Devel module, and Drupal API reference.
- Form API - simplifies working with forms. Forms described consistently as PHP arrays, have control over access, validation, and submission, and provide added security
Demo, watched but did not take notes
Changes in Drupal 7