Creating Simple Drupal Module to display form - Coding Cyber

Creating Simple Drupal Module to display form

This is simple Drupal Module to display form, before creating module we need to create module directory under your-drupal-installation\all\sites\modules, Create folder with module name, here i’m creating module name with “codingcyber“. In that folder create two more files

  1. codingcyber.info – this is where the information about your module, this information will be displayed in the modules page.
  2. codingcyber.module – this is where all your module functionality will be.

I have written this on drupal version 6, i don’t know whether it works on drupal version 7 or not.

codingcyber.info

[php]name = Coding Cyber
description = A description of what your module does.
core = 6.x[/php]

codingcyber.module

This code will be in separate pieces add them to single file.

This bit of code dispalys the module help

[php] <?php
/**
* Display help and module information
* @param path which path of the site we’re displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function codingcyber_help($path, $arg) {
$output = ”; //declare your output variable
switch ($path) {
case "admin/help#codingcyber":
$output = ‘<p>’. t("Displays form") .'</p>’;
break;
}
return $output;
} // function codingcyber_help
[/php]

This code defines module permissions

[php] /**
* Valid permissions for this module
* @return array An array of valid permissions for the codingcyber module
*/
function codingcyber_perm() {
return array(‘access formmodule’);
} // function codingcyber_perm()
[/php]

This code defines menu link

[php] function codingcyber_menu() {
$items = array();

$items[‘codingcyber/form’] = array(
‘title’ => t(‘Custom User Registration’),
‘page callback’ => ‘drupal_get_form’,
‘page arguments’ => array(‘codingcyber_form’),
‘access arguments’ => array(‘access formmodule’),
‘type’ => ‘MENU_CALLBACK’,
);
return $items;
}
[/php]

This code displays the form.

[php] function codingcyber_form() {
$form[‘username’] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘User Name’),
‘#required’ => TRUE,
);
$form[‘password’] = array(
‘#type’ => ‘password’,
‘#title’ => t(‘Password’),
‘#required’ => TRUE,
);
$form[‘submit’] = array(
‘#type’ => ‘submit’,
‘#value’ => t(‘Submit’)
);
return $form;
}
[/php]

This is where the functionlity of the module resides.

[php] function codingcyber_form_submit($form, &$form_state) {
// Here is where your submit functionality should go.
}
[/php]

After adding code, enable the module, after enabling the module navigate to help section you can see a link with coding cyber title, after that navigate to drupal-installation-directory/codingcyber/form, Now you can see the form, you can see it from below screen shots.

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: