How to make a simple form module in Drupal

This example module will provide a form where you can enter your name and upon submission will display your name as a message on the following page. It is very basic but will give you a start to making your first module. You can change all instances of the word example to the name of your module. First you need to create a file called example.info:


1name = Example


2description = "Example module."


3version = "5.x-1.0"


4project = "example"
Next you need the example.module file:


01<?php


02 


03function example_help($section) {


04  switch ($section) {


05    case 'admin/modules#description':


06      return t('This module implements an example form.');


07  }


08}


09 


10function example_menu($may_cache) {


11  $items = array();


12  if ($may_cache) {


13    $items[] = array(


14      'path' => 'example',


15      'title' => t('Example'),


16      'callback' => 'example_page',


17      'access' => TRUE,


18      'type' => MENU_CALLBACK


19    );


20  }


21  return $items;


22}


23 


24function example_page() {


25  return drupal_get_form('example_page_form');


26}


27 


28function example_page_form() {


29  $form['fullname'] = array(


30    '#type' => 'textfield',


31    '#title' => t('Enter your full name'),


32  );


33  $form['submit'] = array(


34    '#type' => 'submit',


35    '#value' => t('Save'),


36  );


37  return $form;


38}


39 


40function example_page_form_submit($form_id, $form_values) {


41  $message = 'You have submitted the ' . $form_id . ' form which contains the following data:<pre>' . print_r($form_values,true) . '</pre>';


42  drupal_set_message(t($message));


43}


44 


45?>
Now you can enable the module, and visit:
0 Comments
Disqus
Fb Comments
Comments :

0 comments:

Post a Comment