سؤال

I was searching online for a tutorial to create a form which is displyaed on a page, Shall we display a form inside a module as we display content using module and block? Since I am new to drupal I do not have an idea about drupal form. I downloaded and installed the example form module. But I do not know where this form will get displayed. I downloaded it from here http://drupal.org/node/1121110

هل كانت مفيدة؟

المحلول

It's not that complicated even if you are new to drupal. All I had to do in this example is using hook_menu() and knowing available form items from drupal form api reference.

Below is an example for what you're trying to do.

/**
 * Implementation of hook_menu()
 */
function mymodule_menu()
{
    $items = array();

    $items['my-custom-page-path'] = array(
        'title'             => 'My Page Title',
        'description'       => t(''),
        'access callback'   => 'user_access',
        'access arguments'  => array('access content'),
        'page callback'     => 'drupal_get_form',
        'page arguments'    => array('mymodule_form_id'),
    );

    return $items;
}

function mymodule_form_id($form, &$form_state)
{
    $form = array();

    $form['my_textfield'] = array(
        '#type'         => 'textfield',
        '#title'        => t('Text Field'),
        '#description'  => t(''),
        '#weight'       => 20,
        '#required'     => TRUE,
        '#size'         => 5,
        '#maxlength'    => 5,
    );

    $form['submit'] = array(
        '#type'         => 'submit',
        '#value'        => t('Save settings'),
        '#weight'       => 10000,
    );

    return $form;
}

/**
* Form validation callback
*/
function mymodule_form_id_validate($form, &$form_state)
{
    // notice adding "_validate" to the form id
}

/**
* Form submission callback
*/
function mymodule_form_id_submit($form, &$form_state)
{
    // notice adding "_submit" to the form id
}

نصائح أخرى

#Here is the simple code for creating form in module#

===============================================================

/*..firstly create a menu in module by copying this code..*/
function form_test_menu() {
    $items['formtest'] = array(
                        'title' => 'Form Test',
                        'page callback' => 'drupal_get_form',
                        'page arguments' => array('form_test_form'),
                        'access callback' => TRUE,
                        );
    return $items;
}

/*...Now create fields like below...*/
function form_test_form($form,&$form_submit) {
    $form['firstname'] = array(
                            '#title' => t('Firstname'),
                            '#type' => 'textfield',
                            '#required' => TRUE,
                            );
    $form['lastname'] = array(
                            '#title' => t('Lastname'),
                            '#type' => 'textfield',
                            );
    $form['submit'] = array(
                        '#value' => 'Submit',
                        '#type' => 'submit',
                        );
    return $form;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top