Tag

Custom Module hook

Browsing

How To Create Custom Module Form with Menu Link in Drupal

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 .

name = "Custom Modules"
core ="7.x"
description = "This is the custom modules for Form and Menu."

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

<?php

function custom_module_form($form, &$form_state)
// $form as input
// &$form_state is reference
{
// Assosiative array to get the form fields
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
);
// Form field name and name as type and its value
// type and value refferred as # sign
return $form; // get or print the results
}

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.

function custom_module_form($form, &$form_state)
// $form as input
// &$form_state is reference
{
// Assosiative array to get the form fields
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
);
// Form field name and name as type and its value
// type and value refferred as # sign
return $form; // get or print the results
}
function custom_module_form_validate($form,&$form_state)
{
}
function custom_module_form_submit($form,&$form_state)
{
}

$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.

function custom_module_menu()
{
$items = array();

$items['custom/custom-form']= array( //this creates a URL that will call this form at "custom/custom-form"
'title' => 'Custom Modules', //page title
'description' => 'This is custom modules in page',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_module_form'), // Form name
'access callback' => True
);
return $items;
}

‘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

function custom_module_menu()
{
$items = array();

$items['custom/custom-form']= array(
'title' => 'Custom Modules',
'description' => 'This is custom modules in page',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_module_form'),
'access callback' => True
);
return $items;
}

function custom_module_form($form, &$form_state)
// $form as input
// &$form_state is reference
{
// Assosiative array to get the form fields
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
);

// Form field name and name as type and its value
// type and value refferred as # sign

return $form; // get or print the results

// Need to steps using drupal hook
// 1. Form validations
// 2. Form submit

function custom_module_form_validate($form,&$form_state)
{
// Form_set_error is validation for drupal form
}
function custom_module_form_submit($form,&$form_state)
{

}
// Need to add From menu link using hook
}

Step 5: Add More Fields in the Form.

Now we will add one more field to the form.

function custom_module_form($form, &$form_state)
// $form as input
// &$form_state is reference
{
// Assosiative array to get the form fields
// Assosiative array to get the form fields
$form['firstname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#required' => True,
);
$form['lastaname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#required' => True,
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Me'),
);

// Form field name and name as type and its value
// type and value refferred as # sign

return $form; // get or print the results

// Need to steps using drupal hook
// 1. Form validations
// 2. Form submit

function custom_module_form_validate($form,&$form_state)
{
// Form_set_error is validation for drupal form

}
function custom_module_form_submit($form,&$form_state)
{

}
// Need to add From menu link using hook

}

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

Custom Form ModulesStep 6: Add the validation function in form

function custom_module_form_validate($form,&$form_state)
{
if (!($form_state['values']['firstname'] > 0)){
form_set_error('firstname', t('Firstname Field is required.'));
}
if (!($form_state['values']['lastaname'] > 0)){
form_set_error('lastaname', t('Lastaname Field is required.'));
}
// Form_set_error is validation for drupal 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.