Building out Drupal module interfaces with a stub function
A technique that I’m finding quite useful during module development is to create a “stub” function for use as a generic interface callback for menu items that I want to define, but haven’t yet written callbacks for. For example, if I were building a Drupal 5 module called widgetmaster
, I would define:
function _widgetmaster_stub($message = "This feature is still<br> in development.") {<br> return '<em>'. t($message) .'</em>;<br>}<br>
So if I’m planning an admin section for my module but haven’t gotten around to implementing it yet, I can at least build out the menu so I (or a client) can click around and make sure the interaction and flow makes sense… for example:
/**<br> * Implementation of hook_menu().<br> */<br>function widgetmaster_menu($may_cache) {<br> $items = array();<br> $access = user_access('access administration pages');<br> if ($may_cache) {<br> $items[] = array(<br> 'title' => t('Widgetmaster Settings'),<br> 'path' => 'admin/settings/widgetmaster',<br> 'callback' => '_widgetmaster_stub',<br> 'type' => MENU_NORMAL_ITEM,<br> 'access' => $access<br> );<br><br> $items[] = array(<br> 'title' => t('Overview'),<br> 'path' => 'admin/settings/widgetmaster/overview',<br> 'callback' => '_widgetmaster_stub',<br> 'type' => MENU_DEFAULT_LOCAL_TASK,<br> 'access' => $access<br> );<br><br> $items[] = array(<br> 'title' => t('Advanced'),<br> 'path' => 'admin/settings/widgetmaster/requiredfields',<br> 'callback' => '_widgetmaster_stub',<br> 'type' => MENU_LOCAL_TASK,<br> 'access' => $access<br> );<br>}<br>
I can also leave myself specific notes as to what a particular menu callback is supposed to do:
...<br><br>$items[] = array(<br> 'title' => t('Advanced'),<br> 'path' => 'admin/settings/widgetmaster/requiredfields',<br> 'callback' => '_widgetmaster_stub',<br> 'callback arguments' => array('Select advanced options from<br> the widgetmaster_frob table and display as a set of <br> checkboxes');<br> 'type' => MENU_LOCAL_TASK,<br> 'access' => $access<br>);<br><br>...<br>
Usage needn’t be limited to menu callbacks, either; it’s handy wherever you know you’ll eventually be defining a function that returns a string.
I’m finding it very helpful to have these stub interfaces in place early on in development. It helps fine-tune interaction, and it also serves as a contextual TODO list.
Tags: DrupalmoduledevelopmentDrupal5
-
var switchTo5x=true;stLight.options({publisher:'dr-8d4fe24c-a8ab-ba90-8086-3791b02244be'});