Doxygen formatting conventions
Doxygen is a documentation generation system. The documentation is extracted directly from the sources, which makes it much easier to keep the documentation consistent with the source code.
There is an excellent Doxygen manual at the Doxygen site. The following notes pertain to the Drupal implementation of Doxygen.
General documentation syntax
To document a block of code, the syntax we use is:
/** * Documentation here. */
Doxygen will parse any comments located in such a block. Our style is to use as few Doxygen-specific commands as possible, so as to keep the source legible. Any mentions of functions or file names within the documentation will automatically link to the referenced code, so typically no markup need be introduced to produce links.
Documenting files
It is good practice to provide a comment describing what a file does at the start of it. For example:
<?php // $Id: theme.inc,v 1.202 2004/07/08 16:08:21 dries Exp $ /** * @file * The theme system, which controls the output of Drupal. * * The theme system allows for nearly all output of the Drupal system to be * customized by user themes. */
The line immediately following the @file directive is a short description that will be shown in the list of all files in the generated documentation. If the line begins with a verb, that verb should be in present tense, e.g., "Handles file uploads." Further description may follow after a blank line.
To add CVS ID-Tags to your file, add a // $Id$ to your file. CVS will automatically expand it to the format shown above. In the future, you don't have to care about that as CVS will update these information automatically.
For .install files, the following template is used:
/** * @file * Install, update and uninstall functions for the XXX module. */
Documenting functions
All functions that may be called by other files should be documented; private functions optionally may be documented as well. A function documentation block should immediately precede the declaration of the function itself, like so:
/**
* Verifies the syntax of the given e-mail address.
*
* Empty e-mail addresses are allowed. See RFC 2822 for details.
*
* @param $mail
* A string containing an email address.
* @return
* TRUE if the address is in a valid format.
*/
function valid_email_address($mail) {
The first line of the block should contain a brief description of what the function does, limited to 80 characters, and beginning with a verb in the form "Does such and such" (third person, as in "This function does such and such", rather than second person imperative "Do such and such"). A longer description with usage notes should follow after a blank line, if more explanation is needed. Each parameter should be listed with a @param directive, with a description indented on the following line. After all the parameters, a @return directive should be used to document the return value if there is one. There is no blank line between the @param and @return directives.
Functions that are easily described in one line may omit these directives, as follows:
/**
* Converts an associative array to an anonymous object.
*/
function array2object($array) {
The parameters and return value must be described within this one-line description in this case.
Documenting hook implementations
Many modules consist largely of hook implementations. If the implementation is rather standard and does not require more explanation than the hook reference provides, a shorthand documentation form may be used:
/**
* Implements hook_help().
*/
function blog_help($section) {
This generates a link to the hook reference, reminds the developer that this is a hook implementation, and avoids having to document parameters and return values that are the same for every implementation of the hook.
Documenting forms
In order to provide a quick reference for themers, we tag all form builder functions so that Doxygen can group them together. The form builder function is defined as any function meant to be used as an argument for drupal_get_form. To do this, add a grouping instruction to the documentation of the function. Additionally, while submit, validate and other handlers for the form are not meant to be in this group, you should provide a @see to provide an easy reference to handlers that are attached to the form.
/** * FAPI definition for the user login form. * * ... * @ingroup forms * @see user_login_default_validators() * @see user_login_submit() */ function user_login(&$form_state, $msg = '')
Documenting themeable functions
In order to provide a quick reference for theme developers, we tag all themeable functions so that Doxygen can group them on one page. To do this, add a grouping instruction to the documentation of all such functions:
/**
* Formats a query pager.
*
* ...
* @ingroup themeable
*/
function theme_pager($tags = array(), $limit = 10, $element = 0, $attributes = array()) {
...
}
Documenting theme templates
If a template and a preprocess function is used instead of a theming function, an empty function definition of the theme function that is not used should be placed in the contributed documentation (contributions/docs/developer/theme.php).
The template itself should be documented with a @file directive and contain a list of the variables that the template_preprocess_HOOK has prepared for it. If any of these variables contain data that is unsafe to output for XSS reasons, they should be documented; otherwise it can be assumed that variables available have already been appropriately filtered. Anything not listed should not be assumed to be safe to output. It should also contain a @see directive to link back to the preprocessor and the theme_X function.
<?php // $Id$ /** * @file * Default theme implementation to display a list of forums. * * Available variables: * - $forums: An array of forums to display. * * Each $forum in $forums contains: * - $forum->is_container: Is TRUE if the forum can contain other forums. Is * FALSE if the forum can contain only topics. * - $forum->depth: How deep the forum is in the current hierarchy. * - $forum->name: The name of the forum. * - $forum->link: The URL to link to this forum. * - $forum->description: The description of this forum. * - $forum->new_topics: True if the forum contains unread posts. * - $forum->new_url: A URL to the forum's unread posts. * - $forum->new_text: Text for the above URL which tells how many new posts. * - $forum->old_topics: A count of posts that have already been read. * - $forum->num_posts: The total number of posts in the forum. * - $forum->last_reply: Text representing the last time a forum was posted * or commented in. * * @see template_preprocess_forum_list() */
The template_preprocess_HOOK function should also contain appropriate @see directives.
Documenting contributed modules and themes
- Don't use
@mainpage. There can be only one@mainpagein the contributions repository, which is reserved for an index page of all contributes modules and themes. - Use Doxygen Modules (
@defgroup,@ingroup,@addtogroup, see "Limitations and hints" below) sparingly. There are currently over 2,200 module directories in contrib, many of them consisting of more than one module. If each of these modules used just one@defgroup, there would be more than 2,200 entries in the global Module list. If each used more than one ... - If you do use Doxygen Modules, make sure you give them a unique namespace, which would be your module's name. E.g.
@defgroup views ...for the views.module,@defgroup views_ui ...for the views_ui.module. Don't use group names which are defined in Drupal core (hooks, themeable, file, batch, database, forms, form_api, format, image, validation, search, etc.).
A recommended way of using Doxygen grouping in contributed modules and themes is the following:
in your main example.module:
/** * @defgroup example Example: short description of your module * * Longer description of your module, including all other files and modules. */ /** * @file * * Description of your module's main file. * * @ingroup example */
in the other modules and code files of your module:
/** * @file * * Description of another file in your module. * * @ingroup example */
This way, you get all your modules files related together in the group "example".
Limitations and hints
Drupal's Doxygen processing module, api.module, currently only supports a small subset of all Doxygen commands and makes some assumptions about the formatting of the source. Code to be processed by api.module is advised to stick to these conventions.
Api.module currently supports only one of Doxygen's three grouping mechanisms: Modules (@defgroup, @ingroup, @addtogroup, @{, @}). When using those, please note the following:
- Modules
work at a global level, creating a new page for each group
. They should be used only to group functions that provide some kind of API, which possibly spans multiple files. Or the other way round: they should not be used to group functions in a file when these functions are only used in that very file. Thats what Member Groups are for (which unfortunately aren't supported by api.module yet). @defgroups can be defined only once - trying to define a second@defgroup namewith a name already used will result in an error. Use@defgroup namein the "most important" section/file of that group and add to it from other places with@addtogroup/@ingroup.- The
namein@defgroup name Explaination of that groupmust be single-word identifier, like a PHP variable or function name. Or, as regular expression:[a-zA-Z_][a-zA-Z0-9_]*. Dots, hyphens, etc. are not allowed.
To see how a real Doxygen processes and displays the current Drupal code documentation (both core and contrib), have a look at ax' Drupal site. Especially, look at the "doxygen error logs" and help improving Drupals code documentation.

Unordered lists
You can create an unordered list in the Doxygen generated documentation by using a dash at the beginning of the line (but after the asterisk), i.e.
* - This line will have a bullet.This is used for example in the documentation of forum_list.tpl.php (see the section above "Documenting theme templates"), and the finished result looks like this http://api.drupal.org/api/file/modules/forum/forum-list.tpl.php/6.
gpk
----
www.alexoria.co.uk
Inserting code examples
Code examples can be embedded in the Doxygen documentation using the following syntax:
/** Prints a string.*
* This is a function that prints a string.
*
* Example usage:
* @code
* my_print('Hello World!');
* @endcode
*
* @param $str
* String to be printed.
*/
function my_print($str) {
print($str);
}
Maybe we could use
Maybe we could use @implements for hook implementations:
Using @defgroup
I'm trying to improve the documentation in my code and have been struggling to understand how/when to use @defgroup and @ingroup. I think I have it, but can someone confirm it.
First, group pages show a table of all functions that are in that group. So if I write a theme function and document it as
@ingroup themeablethen it will appear on the group page and make it easy for someone to see that my module has something themeable in it. Meanwhile the documentation page for the my function will get a nicely formatted link to the group.Since I get a formatted table of functions for my module automatically under
/api/file/some/path/mymodule.moduleI don't need a @defgroup. I can see all the functions from the file documentation page.Unless I am expecting other modules to create functions that need to be summarised along with mine I should probably never put a
@defgroupin my module. It makes sense to define a group for something like themes and forms, but it generally wouldn't make sense to define one for a module. In fact, unless some other module declares something to be@ingroup mygroupthere would never be anything on the group page except my own module's functions.Once you have defined a @defgroup then all functions that appear in the file after the definition will belong to that group. theme.inc is a good example of this. The defgroup comes about halfway through the file, and all theme_element() functions come after it. All other functions are defined before.
Second, I found that although the documentation suggests using @see over several lines is ok
@see drupal_render()@see hook_theme()
It actually results in strange results on the finished page. Multiple @see should be written
@see drupal_render(), hook_theme(). I don't think this is just me, you can see this error on, for example, http://api.drupal.org/api/group/themeable/6.Third, I didn't find this written anywhere, but if I include a filename in the doc comments then it will be turned in to a link to that file. So if I put the bulk of my "common" documentation in to @file then it can be cross referenced simply by writing "This is described in mymodule.module". This is illustrated quite nicely in theme.inc where the @defgroup links back to the page for theme.inc itself.
Finally, functions should be described using present tense and start with a verb - 'Strip bad characters from a string', or 'Theme an item of content'. From looking more closely at the produced tables this makes the function summaries much easier to read. Again, the themeable group illustrates this and has examples of good and bad - is it just me or should "This function formats an administrative block for display." be "Format an administrative block for display." if it were to follow the proper convention? I didn't fully appreciate how the api pages work as to why the "verb" convention makes sense.
And really finally (my daughter has now gone back to sleep and it's 3am) - constants can be documented simply by putting a doc block above them. I didn't know that! It will produce another nice table on the page describing the file (e.g. http://api.drupal.org/api/file/modules/block/block.module/6), and it will also add the constant to the summary table on
/api/constants.So, I'm going to start documenting on the basis of the above, pending clarification!
If the above is right, and the descriptions make sense, I'm happy to roll a version of this on the main page.