Multipage form in custom module
I have created a custom module (natpages) for a new content type called 'natpages'.
In the module i have created various forms using FAPI where the user can enter content into the website.
The form was a single page form and it worked perfectly and i was able to create pages using this module.
But then I had to modify the module so that its is able to handle the same content in different languages.
The only way that im know of implementing this is to create a multipage form.
The way i tried to implement is to first have a form where the user enters the number of languages (n) in which it
wants the content and then create 'n' number pages which will consist the same forms.
I was partially successful in the first part, where the users would enter the number of languages.
But the problem here is that when i click on 'submit' instead of going to the next page, which should contain forms where users can input content.
I again get back the same form and even though i have a custom hook_submit function the node_form_submit function in node.module creates a new node.
Since the first form page is only for the number of languages(i.e forms the module should create). I dont want to create a node until the user
reaches step 2 (i.e the form where users can input content).
This is my hook_form and hook_submit functions:
function natpages_form(&$node){
$form = array();
$form['#multistep'] = TRUE;
$form['#redirect'] = FALSE;
$languageoptions = array(
'af' => 'Afrikaans',
'sq' => 'Albanian',
'ur' => 'Urdu',
've' => 'Venda',
'vi' => 'Vietnamese',
'xh' => 'Xhosa',
'ji' => 'Yiddish',
'zu' => 'Zulu'
);
drupal_set_message(" hook_form is called ");
$type = node_get_types('type',$node);
if (!$node->numberoflanguages)
{
drupal_set_message('Getting the Langauge Information');
$form['countryinfo'] = array(
'#title' => t('Country Information'),
'#type' => 'fieldset',
'#description' => t("Country Languages"),
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['countryinfo']['numberoflanguages'] = array(
'#title' => t('Enter the number of country languages'),
'#type' => 'textfield',
'#default_value' => $node->numberoflanguages,
'#required' => TRUE
);
}
else
{
drupal_set_message('Enter Information');
$form['body']= array(
'#type' => 'hidden',
'#default_value' => $node->maincontent,
);
$form['photocaption'] = array(
'#title' => t('Photograph Information'),
'#type' => 'fieldset',
'#description' => t("Information about the Feature and Historic Photograph"),
'#collapsible' => TRUE,
'#collapsed' => FALSE
);
$form['photocaption']['featurephotocaption'] = array(
'#type' => 'textarea',
'#title' => t('Feature Photograph Description'),
'#default_value' => $node->featurephotocaption,
'#rows' => 4,
'#default_value' => $node->featurephotocaption,
'#required' => TRUE
);
$form['photocaption']['featurephotocaption']['#id'] = 'featurephotocaption';
$form['photocaption']['historicphotocaption'] = array(
'#type' => 'textarea',
'#title' => t('Historic Photograph Description'),
'#default_value' => $node->historicphotocaption,
'#rows' => 4,
'#default_value' => $node->historicphotocaption,
'#required' => TRUE
);
$form['firstparagraph'] = array(
'#type' => 'textarea',
'#title' => t('Locale Overview Teaser'),
'#default_value' => $node->firstparagraph,
'#rows' => 10,
'#required' => TRUE
);
$form['maincontent'] = array(
'#type' => 'textarea',
'#title' => t('Locale Overview Body'),
'#default_value' => $node->maincontent,
'#rows' => 10,
'#required' => TRUE
);
}
return $form;
}
function natpages_submit($node) {
drupal_set_message("natpages_submit is called ");
switch($node->op) {
case 'Submit':
if(node->maincontent)
{
print 'Content entered so will save node now';
node_save($node);
}
else
{
print 'Content hasnt yet been entered.';
}
break;
}
So in hook_submit even though the content hasnt been entered node_form_submit in node.module creates a node for it.
How can i pass variable between the form pages and not create a new node??
Any help is appreciated, especially after 3 days of frustration with multipage forms :p
Thanks

soln
for anyone who is facing a similar situation as I did one way of creating a multipage form is to follow the following steps:
1) Create 2 "node forms" types in your hook_node_info. This makes it easier when we want to redirect the page to a new page which has a different form.
function natpages_node_info()
{
return array(
'natpages' => array(
'name'=>t('Pages'),
'module' => 'natpages',
'description' => t('Create FIRST a page '),
'has_title' => TRUE,
'title_label' => t('Getting the Country information to create their pages'),
'has_body' => TRUE,
'body_label' => t('Body')
) ,
'languagepage' => array(
'name'=>t(' Pages in different languages'),
'module' => 'natpages',
'description' => t('Create pages.'),
'has_title' => TRUE,
'title_label' => t('Pages in different languages)'),
'has_body' => TRUE,
'body_label' => t('body content of each of the national pages')
)
);
}
2) After submitting the First form (i.e page 1) and inserting content into the database using hook_insert have a drupal_goto at the end of hook_insert which redirects to the 2 "node form" page. Here we use the arg() function to determine into which db table to insert the values
if(arg(2) != 'languagepage')
{
db_query("insert into {table} (nid, vid, other_variables) values ('%s', '%s', '%s')", $node->nid, $node->vid, $node->other_variables);
drupal_goto('node/add/languagepage', "nid=". $node->nid);
}
else //if the page type is language then go to then insert values and go to the created page after inserting into db
{
db_query("insert into {anothertable} (nid, vid, other_variables) values ('%s', '%s', '%s')", $node->nid, $node->vid, $node->other_variables);
drupal_goto('node/%s',$node->nid); // go to the created page
}
3) On being redirected to this new page(i.e page 2) use the arg() function to determine which form you want to display.
if( (arg(1) == 'add' && arg(2) != 'languagepage') || ( arg(2) == 'edit' && $type->type != 'languagepage') )
{
//form code to display when page type is not "language"
}
else
{
//form code to display when page type is "language"
}
The following 3 steps can be iterated for the "n" number of multipage forms you want to create.
The key function that makes all this possible besides the hook functions offcourse is the arg() function and drupal_goto().
Hope this helps someone else who is faced with the same daunting task as I was faced with.
Am looking forward to D6 which makes life much easier when creating multipage forms.