Drupal Specialist Journal

Create a simple Drupal module with Form API, hook_validate and hook_submit

Drupal has many API we can play with. The most useful API is form API. I’ll show how to create a simple Drupal 5 module to add your friend data to database. Just create a MySQL table name “friends” with 3 field: ID, name, and email. Make sure the ID field is auto_increment.

Module can be created under directory: modules or sites/all/modules. I prefer to create new module on sites/all/modules, so our custom module is separated with core module. Create folder mymod on sites/all/modules then create file mymod.module and mymod.info

Write file mymod.info with this content:

name = My Module
description = My first time module
version = "1.0"

The .info file will appear on administration modules page. On this how to I will show you how to create module with hook_validate and hook_submit. You may say, hook is an function depends on what module you use. We create mymod module, so we will create mymod_{FUNCTION_NAME}_validate and mymod_{FUNCTION_NAME}_submit

Next fill the mymod.module with content as follows:

function mymod_add_friend () {
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name',
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE
);


$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#size' => 30,
'#maxlength' => 128,
'#required' => TRUE
);


$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}


function mymod_add_friend_validate($form_id, $form_values) {
if (!empty($form_values['email']) && !valid_email_address ($form_values['email']))
form_set_error('', 'Your email is empty or not valid');
if (empty($form_values['name']))
form_set_error('', 'Your name is empty');
}


function mymod_add_friend_submit($form_id, $form_values) {
db_query ("INSERT INTO {friends} VALUES(0, '%s', '%s')", $form_values['name'], $form_values['email']);
drupal_set_message('Thank you. Your friend has added');
}

The first function builds a form with form API, you call this form by using drupal_get_form function, example:

print drupal_get_form('mymod_add_friend');

The second function check for form input, if the output return false then the form processing is halted and Drupal will show an error message

The third function execute the form submission if there are no errors or validation has passed, use drupal_set_message to display a success message.

Activate your module on administration page the place code print drupal_get_form(’mymod_add_friend’); on your template and you will get a form then congratulation, you have created a Drupal module succesfully!

21 May, 2009

It’s time to move to Drupal 6

Posted by: zayn hamdan In: blog

I love stable application. After 12 maintenance releases, I move from Drupal 5 to 6 version. I have been waiting from the first release of Drupal 6 hit the street, then it’s the time to move forward.

I’m building many websites using drupal, from Drupal 4.7, 5.1x and then now 6.1x, those websites will not be upgraded to 6, because there are many API changes, I realise that when I’m building some websites right now, my custom modules must have an adjustment, especially for API related to theme and form.

Drupal 6 has so many improvements, those improvements can be found by googling ;) . The most important changes for me is an easy to use custom theme. I can alter and change every single part of website by writing a hook_theme function. Some core modules has a predefine template which can be a good starting point to alter it. I will not go into details how to alter, modify the template, there are numerous tutorial out there, all you do just search it with your favorite search engine :)

Start to use drupal 6 now :) , I’m looking forward for Drupal 7, but have to wait until it really, really stable

Categories

Archives

Random Quran


  • gaus surahman: Ah, you answered me :) Thought you were kidding us by claiming a drupal specialist but using WP as your blogging software. I personally think, whe
  • oedin: yah.. yg senasib bnyk jg rupanya. malahan gw gag bs connect2.. br connect kl sinyalnya GPRS. aneh neh indosat.. bilangnya 3G tp koneknya maunya ke g
  • KutuKerah: hm, udah coba pake internet indosat accelerator lom mbak ? sepengalaman saya, dengan make program itu, performanya bisa nambah lo.. mungkin practica

Flickr PhotoStream

  • Luqman main mobil-mobilan sambil bawa bola basketDigitalk 2009 eventLagi pada NakalMaryam, mama, luqmanLuqman di acara ulang tahun 2Luqman di acara ulang tahunAcara ulang tahun anak tetanggaOTW home

About Me

I’m a web enterpreneur and also as a web programmer. Start as a Linux administrator and web programmer in 2005, I worked as a web technology teacher before and then as a freelancer for making web application. See my portofolio. I work for GroupM Interaction Indonesia until now.
I started this blog on 2002, there are [...]

more about me »

Twitter Updates

    Follow me on twitter