A lot of times you have to make the node/add page look just the way you want, and you usually start complaining that it's technically impossible with Drupal. There are a lot of techniques for this but I will be talking about one in particular which is creating your own little module that does the job.
Creating a module? Isn't that really hard?
Not really, it's simpler that it sounds and in this case it won't have more than 60 lines of code.
Our module will be called Apply, and it will basically show a form so users can apply for something. It will create a node based on a custom node type we will create.
- Go to sites/all/modules
- Create a folder called apply
- Create 2 files: apply.module and apply.info
Now we have all the necessary files to create our module, so let's get our favorite text editor and open apply.info and fill some basic information so Drupal can recognize our module.
name = Apply to something description = Let users submit applications to something package = Other core = 6.x dependencies[] = content
That's it on this file. Basically we need a name, a description, which package the module will be under, the Drupal core version needed and the dependencies. For this case we will need the content module (CCK).
Now we open apply.module and the very first thing we add is the menu hook. This hook let us create an specific path on our site and use a callback function to display content on it.
/**
* Implementation of hook_menu().
*/
function apply_menu() {
$items = array();
$items['apply'] = array(
'title' => 'Submit application',
'description' => 'Does something',
'page callback' => 'drupal_get_form',
'page arguments' => array('apply_submit_form'),
'access arguments' => array('access content')
);
return $items;
}
On this case we create the path 'apply' and give it a tittle and description to it. We also specify which permission do you need in order to see this page ('access content' is the most basic permission on Drupal).
As you can see we are using the callback function drupal_get_form. This tells Drupal that the page we will be rendering is a form, and the form we will use is the page argument passed inside an array: array('apply_submit_form').
Building the form
Now it's time to build form we want to display, but first, lets enable our module.

After we enable it we can see a new menu entry at our Navigation Menu:

Now comes the fun part, we need to build our form with Drupal and it's quite simple. Drupal has something called Form API wich helps us to build forms as easy as building arrays. So, we create our form function (which is being called by the menu system):
function apply_submit_form() {
$form = array();
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name'
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => 'Last Name'
);
$form['address'] = array(
'#type' => 'textarea',
'#title' => 'Your full address',
'#description' => 'Please include aptartment number and postal code'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Add yourself'
);
return $form;
}
As you can read, we have 4 elements in our form: Name, Last Name, Address and Submit. The Form API has a lot of more options and you can check them all at the Forms API Reference page at Drupal.org
So, what happens when we press Submit?
Drupal will look for the same function name with a _submit appended, but before that it will try to validate it with another function with _validate appended.
In our case we just want to validate the First name and if that goes well, then we can just let it pass.
function apply_submit_form_validate($form, &$form_state) {
if ($form_state['values']['name'] == '') {
form_set_error('name', 'Please enter your first name.');
}
}
Now that we have the form working and it actually submits, we need to build the submit function. This one will basically get the form variables and do whatever you want with them. In our case we will create a node with it.
We will store the First name as the node title, the address as the body and for the Last Name we use a CCK field. I'm not so sure if this is the best idea but for demonstration purposes we will do it that way.
First, we need to create a node type "apply" and create the necessary field for it. Here's a screenshot of the already created node type:

As we can see the CCK field is called field_last. So now we can procced to create our submit function:
function apply_submit_form_submit($form, &$form_state){
$node = new StdClass();
$node->type = 'apply';
$node->status = 1;
// We are using the title as First Name
$node->title = $form_state['values']['name'];
$node->body = $form_state['values']['address'];
$node->field_last[0]['value'] = $form_state['values']['last_name'];
node_save($node);
drupal_set_message(t('Your application has been saved.'));
}
It's really self explanatory, first we create a node object and then we give it the corresponding values. The only different part can be the CCK field which is called in a different way (the [0] refers to the CCK option to have multiple values per field, which in our case we only allow 1).
And there we have it. If everything goes well we will have a module with 4 functions: the HOOK_menu, apply_submit_form(), apply_submit_form_validate() and apply_submit_form_submit().
Here's how our form will look like:

Now in case you didn't read anything and you prefer to just copy and paste it:
<?php
/**
* Implementation of hook_menu().
*/
function apply_menu() {
$items = array();
$items['apply'] = array(
'title' => 'Submit application',
'description' => 'Does something',
'page callback' => 'drupal_get_form',
'page arguments' => array('apply_submit_form'),
'access arguments' => array('access content')
);
return $items;
}
function apply_submit_form() {
$form = array();
$form['name'] = array(
'#type' => 'textfield',
'#title' => 'Name'
);
$form['last_name'] = array(
'#type' => 'textfield',
'#title' => 'Last Name'
);
$form['address'] = array(
'#type' => 'textarea',
'#title' => 'Your full address',
'#description' => 'Please include aptartment number and postal code'
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Add yourself'
);
return $form;
}
function apply_submit_form_validate($form, &$form_state) {
if ($form_state['values']['name'] == '') {
form_set_error('name', 'Please enter your first name.');
}
}
function apply_submit_form_submit($form, &$form_state){
$node = new StdClass();
$node->type = 'apply';
$node->status = 1;
// We are using the title as First Name
$node->title = $form_state['values']['name'];
$node->body = $form_state['values']['address'];
$node->field_last[0]['value'] = $form_state['values']['last_name'];
node_save($node);
drupal_set_message(t('Your application has been saved.'));
}
I hope this short tutorial helps you get started with building modules for Drupal. Sometimes I find that creating a small module to take care of something specific is way better than dealing with our template.php file or some other solutions.
Post new comment