Drupal Module Building

  • 4,993 views
Uploaded on

Beginners' guide to building a Drupal module. An accompanying example module is available at http://bitbucket.org/jdschroeder/lastfm …

Beginners' guide to building a Drupal module. An accompanying example module is available at http://bitbucket.org/jdschroeder/lastfm

This presentation was given to my co-workers as a hand-on workshop.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,993
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
168
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. BUILD A DRUPAL MODULE A Beginner’s Guide
  • 2. THE PRESENTER
  • 3. Joshua Schroeder B.Mgt. (Information Systems), University of Lethbridge, 2007. Web developer at the University of Lethbridge. Lullabot-trained Drupal developer since 2007. Co-founder of Redwall Studios. Maintainer of two contrib Drupal modules.
  • 4. OVERVIEW
  • 5. Workshop overview Development tools and environment Best practices and coding standards Basic concepts in module structure and Drupal API Construct a custom module using data from an external API
  • 6. Slides and source code Download, clone, or fork the source code at Bitbucket. (Sorry, Wesley. I’d have put it on Github, but you know...) http://bitbucket.org/jdschroeder/lastfm Slides will be posted to SlideShare http://slideshare.net/jdschroeder
  • 7. Are you ready? What you need for this workshop: Last.fm account Last.fm API key Drupal 6 development site
  • 8. DEVELOPMENT TOOLS
  • 9. Drupal! Local development environment Acquia Drupal stack (http://acquia.com/downloads) LAMP, MAMP, WAMP, XAMP, etc. MacPorts
  • 10. Modules Coder (http://drupal.org/project/coder) Devel (http://drupal.org/project/devel) Drush (http://drupal.org/project/drush) Firebug (http://getfirebug.com) Drupal for Firebug (http://drupal.org/project/drupalforfirebug)
  • 11. Software TextMate with Drupal bundle (http://macromates.com & http://drupal.org/project/textmate) Coda and Komodo are other feature-rich options (http://panic.com/coda, http://activestate.com/komodo, and http://www.drupalcoder.com/tags/coda-drupal-plugin)
  • 12. Version control Take your pick. Mercurial CVS SVN Git Bazaar
  • 13. API reference Will become your best friend http://api.drupal.org Some mirrors include contrib modules http://api.lullabot.com http://drupalcontrib.org
  • 14. CODING STANDARDS
  • 15. General considerations Soft tabs, 2 spaces. name_functions_with_underscores <?php print “Don’t omit line-ending semi-colons”; ?> Leave out closing ?> from your php files. Use US spellings for comments and names.
  • 16. Comments Use Doxygen-formatted comment blocks (http://drupal.org/node/1354) Properly documented modules can be included in generated API such as what we see at api.drupal.org (http://drupal.org/project/api) Full Doxygen usage documentation available on the Doxygen web site (http://www.stack.nl/~dimitri/doxygen/docblocks.html)
  • 17. Ensure compliance Use the Coder module to test your code (http://drupal.org/project/coder) Become familiar with the standards on the Drupal web site (http://drupal.org/coding-standards)
  • 18. WHAT’S IN A MODULE?
  • 19. .info and .module Every module contains at minimum: module_name.info Describes what your module is, does, and depends on. module_name.module The functional PHP code for your module.
  • 20. .install module_name.install Can specify tasks to be executed on module installation or upgrade. Necessary for database schema changes.
  • 21. LET’S SET-UP A BASIC MODULE.
  • 22. Functions to get us started l($text, $path, $options = array()) Format an internal Drupal link. t($string, $args = array(), $langcode = NULL) Translate strings to the page language or a given language. variable_get($name, $default) & variable_set($name, $value) Return or set a persistent variable. system_settings_form($form) Add default buttons to a form and set its prefix.
  • 23. FORMS API
  • 24. Code-based tylenol Coding HTML forms is a headache. Validating them is more of a headache. Set a $form array and let Drupal handle it. Full reference on Drupal’s API (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6)
  • 25. Form element example $form['first_name'] = array( '#type' => 'textfield', '#title' => t('First Name'), '#description' => t('Your first name.'), '#size' => 40, '#maxlength' => 255, );
  • 26. HOOKS
  • 27. Drupal’s secret weapon Hooks define ways in which modules can share and implement functionality. For example: Implementing hook_block declares the blocks provided by your module. module_name_block extends the Block module’s functionality.
  • 28. hook_perm Define user permissions. Returns an array of strings that correspond to permission items.
  • 29. hook_menu Define menu items and page callbacks. Returns an array of menu items of paths being registered. Let’s start by creating an admin settings page.
  • 30. hook_block Declare a block or set of blocks. Define listing, settings, and content of blocks. Can specify any number of blocks for our module.
  • 31. hook_theme Be nice to themers! Formatting output should be done with theme functions. We have the option of using override-able theme functions or template files (tpl.php) This allows site builders to style the output of your module.
  • 32. LET’S PUT THESE HOOKS INTO ACTION
  • 33. CACHING
  • 34. Here’s where things get heavy Now we need that .install file with hook_install to create a cache table. Create and retrieve with cache_set and cache_get.
  • 35. EXERCISE
  • 36. Now it’s your turn Create a page or block ... ... that uses a theme function or template file ... ... and uses a method from the Last.fm API (http://www.last.fm/api/intro) Need a challenge? Try creating a block to search for artists.
  • 37. FURTHER REFERENCE
  • 38. Book recommendation Learning Drupal 6 Module Development by Matt Butcher Packt Publishing http://packtpub.com/drupal-6-module-development/ book
  • 39. Online Drupal.org forums http://groups.drupal.org http://drupal.org/contributors-guide http://api.drupal.org ... seriously.
  • 40. These slides represent this presentation as delivered to the U of L web team on December 18, 2009. Joshua Schroeder (http://drupal.org/user/202642)