How To Create Custom Module Form with Menu Link in Drupal

By | April 4, 2018

How To Create Custom Module Form with Menu Link in Drupal

This is the Tutorial for beginners who are interested to create a custom Module Form with Custom menu link in drupal. We will look into step by step process. This tutorials will go creation, form creation, and the menu hook function.

Step 1: Creating the Custom module Folder

In first step we are going to creating the custom module folder in the module folder. You should put this in “sites/all/modules/{your module name}.”
here {your module name} is your custom module name. Here im going to create module with name as “custom module”

Now, We have to create two text files called “{name of your module}.module” and “{name of your module}.info.” ie is custom_module.module and custom_module.info

These two files are required by your module. The .info file contains information about what your module does, while the .module file contains the actual code including hooks.

For menu we used drupal hook funtions like hook_menu. Here “hook” is your custom module name as hook. ie custom_module_menu() is the hook function for menu.

Hooks are functions with special names that will be called by Drupal. 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.

Step 2: Creating the module and info files

As we already knew the .info file is saved as custom_module.info. In the file, we write the information about the module like, name, description, core, version, libraries, etc .

We may add more options in the page.

Second, custom_module.module, This is the file we have to create a basic things and functions.

Here we going to create a basic submit form

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.

In the coding
custom_module_form is the hook for the custom form.
To add a field, you set the variable $form[{name of your field}] = array();
$form as input and &$form_state is reference of the form.
Form field name and name as type and its value and type and value referred as # sign.
t() will output text and is a Drupal function that plays nice with translation.

Custom module drupal

Step 3: Creating the validate and submit Functions.

$form and &$form_state are same for all functions. $form is the original form information and can be seen as the original structure of the form. $form_state holds all of the submitted values as well as other information, some of which you can add yourself.

Step 4: Creating Custom Menu link for form.

‘drupal_get_form’ is the function that will be called when the page is accessed. for a form, use drupal_get_form

Custom Module drupal 7

so the module page have form and menu

Step 5: Add More Fields in the Form.

Now we will add one more field to the form.

Now we added fields. Need to check the fields empty or not. Required actions.

Custom Form ModulesStep 6: Add the validation function in form

in $form_state[‘values’][{name of your field}]. There are similar variables for all of your fields.

We then set an error with form_set_error. Adding this error will alert Drupal that the form did not pass validation.

Finally, Here we created the form with validation. We will see submissions and mail sending in next tutorials

Thanks, Please subscribe for more articles.