Creating Drupal A Module


Published on

  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Creating Drupal A Module

  1. 1. Creating Drupal Modules<br />A somewhat less then definitive guide to<br />
  2. 2. Who is this bozo?<br />Adam A. Gregory<br />Drupal Developer for 4 years<br />PHP developer for 8+ years<br />Contribute and maintains modules on<br />Currently work as a freelance Drupal ninja.<br />Worked on Drupal sites large and small.<br /><br />
  3. 3. Why create a module?<br />New functionality<br />Override existing functionality<br />Forms<br />Nodes<br />Users<br />Search<br />Theming<br />Combination of the two<br />Almost every site needs a custom module<br />
  4. 4. Before you start<br />Is there an existing module<br /> (<br />IRC<br />Mailing list (<br />What you need<br />PHP/SQL/HTML/CSS skill<br />IDE<br />Test site<br />Drupal API (<br />
  5. 5. Setting up<br />File Structure<br />Module Folder (custom_pub)<br />.info file (<br />.module file (custom_pub.module)<br />.install file (custom_pub.install)<br />Other files/folders<br />Theme folder<br />.tpl.php files<br />Css/js folders<br />.inc files <br />
  6. 6. .info File<br />Where Drupal gathers all the knowledge about your module<br />Contains module name, decsription, group, version, requirements and dependencies<br />
  7. 7. ;$Id: $<br />name=Custom Publishing Options<br />description=Adds the ability to add Custom publishing options to the node Add/Edit form.<br />core=6.x<br />package=Other<br />
  8. 8. .module File<br />Must have the same name is .info file<br />Contains the PHP code for your module<br />All core hooks must be included in this file to work<br />Should be kept small as it is loaded on every page load<br />Dynamically loading inc files either via hook_menu or by loading them when a hook is called<br />
  9. 9.
  10. 10. .install File<br />Only needed if you module makes ANY sort of DB changes<br />Invokes hook_install, hook_uninstall, hook_update_##, and hook_schema<br />
  11. 11. <?php<br />//;$Id: custom_pub.install,v 1.1 2010/01/20 21:13:05 arcaneadam Exp $<br />/**<br /> * Implementation of hook_uninstall()<br /> */<br />function custom_pub_uninstall() {<br />$types = variable_get('custom_pub_types', array());<br />$ret=array();<br />foreach ($types as $type => $name) {<br />db_drop_field($ret,'node',$type);<br />}<br />variable_del('custom_pub_types');<br />}<br />
  12. 12. Theres a hook for that<br />Drupal works off a hooking system similar to an Observer pattern.<br />Main hooks most modules use<br />Hook_menu<br />Hook_help<br />Hook_form_alter<br />Hook_user<br />Hook_nodeapi<br />API site can help you find other hooks<br />
  13. 13. Organizing Your Code<br />.info and .module are the only required files for a module<br />Keep html/css/js in separate files and not hard coded into .module files<br />Functions for specific pages should moved into .inc files & loaded dynamically<br />Any calls to module files(.inc, .css, .js) should use drupal_get_path($type, $name) for css/js or module_load_include for php<br />
  14. 14. Organizing your code cont.<br />Remember you shouldn’t hard code things like internal links, file paths, etc as you don’t know what sites will use your module and how. Keep your code dynamic and reusable<br />Keep file sizes small and load them when needed for better performance<br />
  15. 15. Finishing Up<br />Load you module into a test site and enable it.<br />Test! Test! Test!<br />Stuff won’t work, you’ll have errors, and things will go crazy<br />DON’T PANIC!<br />It happens, take your time and debug the issues. Use Google, the API, IRC, Forums, Lists, Groups, etc. that are available for help.<br />
  16. 16. A few words about security?<br />Never, ever under any circumstances trust User Input.<br />Drupal provides many functions and resources to properly secure your code.<br />Visit for more info.<br />
  17. 17. Contributing Back<br /><br />If you have created something that will be widely useful, or al least you think it will be, you may want to get it listed on, but how?<br />The process can be painful, long, and hit and miss. Remember the D.o maintainers are volunteers.<br />Requires knowledge of CVS(Blah!)<br />
  18. 18. Resources<br />API: http://api.lullabot.com<br />Security and coding standards:<br />Module Developers Guide:<br />Developing for Drupal:<br />IRC: #drupal, #drupal-nc<br />Mailing list:<br />