The Examples for Developers Module
An example, like a picture, is worth 1000 words. Until recently Drupal programming examples existed but were spread among documentation pages, blog posts, and the Drupal CVS repository; some searching required. That changed in late 2009 with the introduction of the Examples for Developers module. Its purpose: "to provide high-quality, well-documented API examples for a broad range of Drupal core functionality". Now you can find high-quality, working Drupal 6 and 7 code examples in one place, many with SimpleTests.
The Examples for Developers Module, or simply Examples Module, is actually a collection of modules. Each sub-module contains a single, specific example of how to use an API or implement a feature. At the time of writing there are 17 examples:
|AHAH example||Demonstrate Drupal AHAH forms|
|Batch example||An example outlining how a module can define batch operations.|
|Block example||An example outlining how a module can define blocks.|
|E-mail example||Demonstrate Drupal's e-mail APIs.|
|Element example||An example demonstrating how a module can define custom form elements.|
|Filter example||An example module showing how to define a custom filter.|
|Form example||Examples of using the Drupal Form API.|
|Menu example||An example of advanced uses of the menu APIs.|
|Node access example||An example illustrating how to restrict access to nodes based on some criterion associated with the user.|
|Node example||An example module showing how define a custom content type.|
|NodeAPI example||An example module showing how to extend existing content types.|
|Page example||An example module showing how to define a page to be displayed to the user at a given URL.|
|Scaffolding example||Provides example scaffolding for a module that maintains its own database table, and exposes an editing interface for the module's data.|
|SimpleTest example||Used to demonstrate the SimpleTest test suite.|
|Trigger example||An example showing how a module can provide triggers that can have actions associated with them.|
|Vertical tabs example||Show how to use vertical tabs for enhancing user experience.|
|XMLRPC example||This is an example of how to implement XML-RPC callbacks by implementing a validation suite.|
These modules are not just code fragments, they're working examples. Once a specific example module is enabled on the modules admin page (/admin/build/modules) it appears as a menu item in the Admin Menu and can be viewed as a working Drupal web page.
AHAH example page
Some examples, such as the scaffolding example, also create other administration pages.
Scaffolding example Admin page
The Examples module may not answer all of your coding questions but it does provide a high-quality starting point. Many thanks to Randy Fay for initiating this project and to the many people who have added and improved the examples.