How do I create a module configuration page?

admin's picture

Sunday, February 8th, 2009

A customized Drupal module won’t be very useful if the admin user can’t configure it to their needs. This usually consists of several form fields where variables and inputs can be set. Within the module these fields are created within a function, however unlike other common module functions this one is not a hook and does not need a specific function name (although the names are usually along the lines of testmodule_admin or testmodule_settings). The following is an example of the structure these functions can take.

function testmodule_admin_settings() { $form[‘testmodule_nodeno’] = array( ‘#type’ => ‘textfield’, ‘#title’ => t(‘Number of nodes to include’), ‘#default_value’ => variable_get(‘testmodule_nodeno’, 5), ‘#size’ => 2, ‘#maxlength’ => 2, ‘#description’ => t(‘The number of nodes to display.’), ‘#required’ => true, ); return system_settings_form($form); }

No HTML has to be coded to create the form, as this is all done by Drupal. The systems_settings_form function is used to add default buttons and set a form prefix. Note that variable_get is used to set the default value. However, these settings mean nothing if they don’t interact with how the main module displays its contents. In most cases this involves grabbing information from the database – this information needs to conform to our configuration settings. In this example, we want to limit the number of nodes retrieved to 5.

$nodesmax = variable_get(‘testmodule_nodeno’, 5); $sql = ‘SELECT nid, title FROM {node}’; $result = db_query_range($sql, 0, $nodesmax);

To add this page to the sites menu system we must make use of hook_menu. Here we can set the page path, title and permissions.

function testmodule_menu() { $items = array(); $items[‘admin/settings/testmodule’] = array ( ‘title’ => ‘Test module settings’, ‘description’ => ‘Set a node limit to your test module.’, ‘page callback’ => ‘drupal_get_form’, ‘page arguments’ => array(‘testmodule_admin_settings’), ‘access arguments’ => array(‘access administration pages’), ‘type’ => MENU_NORMAL_ITEM, ); return $item; }

If you have already installed and enabled your module, it may be worth disabling and enabling it again for the new settings page to work properly. Resource: http://drupal.org/node/206761 Posted in Web development, Drupal, Drupal modules, Drupal customisation, Drupal development, Drupal configuration | Edit | Comments Off

104 people like this

Comments

Kevin Harris's picture
Comment © BrightLemon ™ web design london

Hello, I've been searching

Hello, I've been searching online about configuration page. Then i found this blog,
you have such great article detailed the steps in creating a module configuration page. I'm so thankful I found this page. Good Job!

Sowmya's picture
Comment © BrightLemon ™ web design london

Thanks for this article. It

Thanks for this article. It really helped me a lot .