Pages

Monday, September 17, 2012

Create a Simple Form in Druapl 7.x - Step by Step Guide



In the Last posts, we have discussed more about create a Hello World Module and Create a content in Drupal 7.x. now we are going to learn and Create a Simple Form in Drupal 7.x step by step.

 Before start works, you must know How does it work? Drupal has several built in Form functions, that deal with building and rendering your forms, and all you have to do is provide an array of information to the build functions. In this case, we build the form array, then return it.

When we call drupal_get_form, we pass the name of the function that returns the form, and Drupal does the rest.

Here the form array is 
$form = array();

Lets begin your simple form,


Step 1: Creating the module structure
The first step in creating the module is to create a folder for the module. You should put this in "sites/all/modules/{name of your module}." 
do not forget to replace the {name of your module} with name of your module. 

For example, we will use the module directory "sites/all/modules/example."

Creates TWO files as like below,

"{name of your module}.module", "{name of your module}.info".

Step 2: The .info file contains information about what your module does, while the .module file contains the actual code including hooks. 

Hooks are functions with special names that will be called by Drupal when actions occur. For example "example_form_menu()" is called when menu information is gathered. 

In many examples, you will see names such as "hook_menu()" or "my_theme_menu()" and in these cases "my_theme" and "hook" should be replaced by the name of your module or theme.

example.info


Create a Simple From in Drupal 7.x



Step 3: Here is the code of your example_form.module file. This will create a very basic form that only has one field and submit button.


Create a Simple From in Drupal 7.x


Note: Do not include the last "?>" part of the PHP code and leave a trailing blank line at the end of the file. This is the Drupal standard.

It is important to note that the function takes the inputs $form, and a reference &$form_state. This function will be called when Drupal tries to build your form.

The process for creating a form is mainly two steps. The first is to build an associative array $form that contains all the fields. The second step is to return that $form variable.

To add a field, you set the variable $form[{name of your field}] = array(); Each field can then have a number of attributes, such as "#type" and "#value." Most of these attributes are named with a proceeding "#" sign.

Step 4: What to do when your form is submitted?
From this point, you will want to either save the form data, send an email, or display some information. That is beyond the scope of this How To at this point.

Now your simple form module is ready to use, upload into your 

sites/all/modules/{your_module_dir}

Go to the Admin modules section, enable it and try with 

http://yoursite.com/{your-module-name} here http://yoursite.com/example

Congratulation, you have successfully created a simple custom form in drupal 7.x. to know more about Drupal and other PHP related CMS, subscribe with us...



Related Posts




4 comments:

  1. I'm new in Drupal. I tried with this code. but when I activate this module nothing work neither its selected.
    why?? what is the solutions.

    ReplyDelete
  2. Again I tried and get some notice and warnings. but I can't understand that how I display this form into a page. and submit it. please help.

    ReplyDelete