开发者

How to develop custom forms for Drupal's admin users?

which will be the best way to develop custom forms for drupal, for admin's part of the sys开发者_运维百科tem?

thank you in advance!


First thing, you need a location to access your form from, preferably in the "admin/*" namespace if the form is only meant for administration. If you're just showing a form, you could directly use drupal_get_form as page callback (but you could use any function to generate the HTML code, even mix with theme functions)

Also, you need to know which permission(s) is required to access the form. By default, I used "access administration pages" but you probably should use something more specific, depending on what you intend the form for.

Let's say the path is "admin/build/something", you need to use hook_menu to register that path:

/**
 * Implementation of hook_menu().
 */
function modulename_menu(){
    return array(
        'admin/build/something' => array(
            'title' => 'Example Admin Form',
            'description' => 'Admin form introduced by the MODULENAME module',
            'type' => MENU_NORMAL_ITEM,
            'page callback' => 'drupal_get_form',
            'access arguments' => array('access administration pages'),
        ),
    );
}

Now, to actually display a page: the value provided in "page arguments" was the name of the function that drupal_get_form expects to provide the form structure (which must be an associative array):

/**
 * Form Structure
 */
function modulename_form_something(&$form_state){
    $form = array();
    $form['myfield'] = array(
        '#title' => 'My Field',
        '#description' => 'This is a basic text input field',
        '#type' => 'textfield',
        '#default_value' => $form_state['values']['myfield'],
    );
    //
    // Here you can add more elements in the form
    //
    return $form;
}

Here is more informations about the Forms API, which you can use to make some pretty complex forms easily.


Now your form is displayed at "/admin/build/something", but you probably want to do soemthing with these data as well; by default, the validate and submit functions are named the same as the form structure function, with "_validate" and "_submit" respectively (however you can override this with #validate and #submit in the form structure).

For example, let's say the string "no" is not a valid value, everything else is accepted.

/**
 * Form validation
 */
function modulename_form_something_validate($form, &$form_state){
    if ($form_state['values']['myfield'] == 'no'){
        form_set_error('myfield', '"<b>no</b>" is not a valid answer, try again.');
    }
}

The validation is called first, however you should only check if data are alright in that function. If you need to perform actions when the form is received, do it in the "submit" handler instead because validate may be called several times while submit is called only once.

/**
 * Form submission
 */
function modulename_form_something_submit(&$form, &$form_state){
    //
    // Here you can perform whatever action that form is made for.
    //
    drupal_set_message( 'The form has been sent. "myfield" has the following value: '.$form_state['values']['myfield'] );
}

Let's summarize, here's the whole modulename.module file:

<?php

/**
 * Implementation of hook_menu().
 */
function modulename_menu(){
    return array(
        'admin/build/something' => array(
            'title' => 'Example Admin Form',
            'description' => 'Admin form introduced by the MODULENAME module',
            'type' => MENU_NORMAL_ITEM,
            'page callback' => 'drupal_get_form',
            'page arguments' => 'modulename_form_something',
            'access arguments' => array('access administration pages'),
        ),
    );
}

/**
 * Form Structure
 */
function modulename_form_something(&$form_state){
    $form = array();
    $form['myfield'] = array(
        '#title' => 'My Field',
        '#description' => 'This is a basic text input field',
        '#type' => 'textfield',
        '#default_value' => $form_state['values']['myfield'],
    );
    //
    // Here you can add more elements in the form
    //
    return $form;
}


/**
 * Form validation
 */
function modulename_form_something_validate($form, &$form_state){
    if ($form_state['values']['myfield'] == 'no'){
        form_set_error('myfield', '"<b>no</b>" is not a valid answer, try again.');
    }
}


/**
 * Form submission
 */
function modulename_form_something_submit(&$form, &$form_state){
    //
    // Here you can perform whatever action that form is made for.
    //
    drupal_set_message( 'The form has been sent. "myfield" has the following value: '.$form_state['values']['myfield'] );
}

Don't forget you also need a .info file for being able to install the module:

Source of modulename.info:

; $Id
name = Admin Form
description = This module adds an admin form
package = Example Module
core = "6.x"
version = "6.x-0.1-dev"


The Drupal form api system will help you make any form you need. If you need to store settings, system_settings_form is a nice shortcut.

The only difference when making admin forms, is to remember to set some sort of permission required, and to place the form somewhere in the /admin/ section of the site. There really isn't anything special about admin forms.


Unless I'm misunderstanding your question, I think you can avoid the hassle of Forms API by using the Webform module.

No code required, nice UI and built in statistics tools.

http://drupal.org/project/webform

Watch a couple tutorial videos and you'll be making just about any form in no time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜