Drupal Ctools Form Wizard in a Block
- by Iamjon
Hi everyone
I created a custom module that has a Ctools multi step form. It's basically a copy of http://www.nicklewis.org/using-chaos-tools-form-wizard-build-multistep-forms-drupal-6.
The form works. I can see it if I got to the url i made for it.
For the life of me I can't get the multistep form to show up in a block.
Any clues?
/**
* Implementation of hook_block()
* */
function mycrazymodule_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('SFT Getting Started');
$blocks[1]['info'] = t('SFT Contact US');
$blocks[2]['info'] = t('SFT News Letter');
return $blocks;
case 'view':
switch ($delta){
case '0':
$block['subject'] = t('SFT Getting Started Subject');
$block['content'] = mycrazymodule_wizard();
break;
case '1':
$block['subject'] = t('SFT Contact US Subject');
$block['content'] = t('SFT Contact US content');
break;
case '2':
$block['subject'] = t('SFT News Letter Subject');
$block['content'] = t('SFT News Letter cONTENT');
break;
}
return $block;
}
}
/**
* Implementation of hook_menu().
*/
function mycrazymodule_menu() {
$items['hellocowboy'] = array(
'title' = 'Two Step Form',
'page callback' = 'mycrazymodule_wizard',
'access arguments' = array('access content')
);
return $items;
}
/**
* menu callback for the multistep form
* step is whatever arg one is -- and will refer to the keys listed in
* $form_info['order'], and $form_info['forms'] arrays
*/
function mycrazymodule_wizard() {
$step = arg(1);
// required includes for wizard
$form_state = array();
ctools_include('wizard');
ctools_include('object-cache');
// The array that will hold the two forms and their options
$form_info = array(
'id' = 'getting_started',
'path' = "hellocowboy/%step",
'show trail' = FALSE,
'show back' = FALSE,
'show cancel' = false,
'show return' =false,
'next text' = 'Submit',
'next callback' = 'getting_started_add_subtask_next',
'finish callback' = 'getting_started_add_subtask_finish',
'return callback' = 'getting_started_add_subtask_finish',
'order' = array(
'basic' = t('Step 1: Basic Info'),
'lecture' = t('Step 2: Choose Lecture'),
),
'forms' = array(
'basic' = array(
'form id' = 'basic_info_form'
),
'lecture' = array(
'form id' = 'choose_lecture_form'
),
),
);
$form_state = array(
'cache name' = NULL,
);
// no matter the step, you will load your values from the callback page
$getstart = getting_started_get_page_cache(NULL);
if (!$getstart) {
// set form to first step -- we have no data
$step = current(array_keys($form_info['order']));
$getstart = new stdClass();
//create cache
ctools_object_cache_set('getting_started', $form_state['cache name'], $getstart);
//print_r($getstart);
}
//THIS IS WHERE WILL STORE ALL FORM DATA
$form_state['getting_started_obj'] = $getstart;
// and this is the witchcraft that makes it work
$output = ctools_wizard_multistep_form($form_info, $step, $form_state);
return $output;
}
function basic_info_form(&$form, &$form_state){
$getstart = &$form_state['getting_started_obj'];
$form['firstname'] = array(
'#weight' = '0',
'#type' = 'textfield',
'#title' = t('firstname'),
'#size' = 60,
'#maxlength' = 255,
'#required' = TRUE,
);
$form['lastname'] = array(
'#weight' = '1',
'#type' = 'textfield',
'#title' = t('lastname'),
'#required' = TRUE,
'#size' = 60,
'#maxlength' = 255,
);
$form['phone'] = array(
'#weight' = '2',
'#type' = 'textfield',
'#title' = t('phone'),
'#required' = TRUE,
'#size' = 60,
'#maxlength' = 255,
);
$form['email'] = array(
'#weight' = '3',
'#type' = 'textfield',
'#title' = t('email'),
'#required' = TRUE,
'#size' = 60,
'#maxlength' = 255,
);
$form['newsletter'] = array(
'#weight' = '4',
'#type' = 'checkbox',
'#title' = t('I would like to receive the newsletter'),
'#required' = TRUE,
'#return_value' = 1,
'#default_value' = 1,
);
$form_state['no buttons'] = TRUE;
}
function basic_info_form_validate(&$form, &$form_state){
$email = $form_state['values']['email'];
$phone = $form_state['values']['phone'];
if(valid_email_address($email) != TRUE){
form_set_error('Dude you have an error', t('Where is your email?'));
}
//if (strlen($phone) 0 && !ereg('^[0-9]{1,3}-[0-9]{3}-[0-9]{3,4}-[0-9]{3,4}$', $phone)) {
//form_set_error('Dude the phone', t('Phone number must be in format xxx-xxx-nnnn-nnnn.'));
//}
}
function basic_info_form_submit(&$form, &$form_state){
//Grab the variables
$firstname =check_plain ($form_state['values']['firstname']);
$lastname = check_plain ($form_state['values']['lastname']);
$email = check_plain ($form_state['values']['email']);
$phone = check_plain ($form_state['values']['phone']);
$newsletter = $form_state['values']['newsletter'];
//Send the form and Grab the lead id
$leadid = send_first_form($lastname, $firstname, $email,$phone, $newsletter);
//Put into form
$form_state['getting_started_obj']-firstname = $firstname;
$form_state['getting_started_obj']-lastname = $lastname;
$form_state['getting_started_obj']-email = $email;
$form_state['getting_started_obj']-phone = $phone;
$form_state['getting_started_obj']-newsletter = $newsletter;
$form_state['getting_started_obj']-leadid = $leadid;
}
function choose_lecture_form(&$form, &$form_state){
$one = 'event 1'
$two = 'event 2'
$three = 'event 3'
$getstart = &$form_state['getting_started_obj'];
$form['lecture'] = array(
'#weight' = '5',
'#default_value' = 'two',
'#options' = array(
'one' = $one,
'two' = $two,
'three' = $three,
),
'#type' = 'radios',
'#title' = t('Select Workshop'),
'#required' = TRUE,
);
$form['attendees'] = array(
'#weight' = '6',
'#default_value' = 'one',
'#options' = array(
'one' = t('I will be arriving alone'),
'two' =t('I will be arriving with a guest'),
),
'#type' = 'radios',
'#title' = t('Attendees'),
'#required' = TRUE,
);
$form_state['no buttons'] = TRUE;
}
/**
* Same idea as previous steps submit
*
*/
function choose_lecture_form_submit(&$form, &$form_state) {
$workshop = $form_state['values']['lecture'];
$leadid = $form_state['getting_started_obj']-leadid;
$attendees = $form_state['values']['attendees'];
$form_state['getting_started_obj']-lecture = $workshop;
$form_state['getting_started_obj']-attendees = $attendees;
send_second_form($workshop, $attendees, $leadid);
}
/*----PART 3 CTOOLS CALLBACKS -- these usually don't have to be very unique ---------------------- */
/**
* Callback generated when the add page process is finished.
* this is where you'd normally save.
*/
function getting_started_add_subtask_finish(&$form_state) {
dpm($form_state);
$getstart = &$form_state['getting_started_obj'];
drupal_set_message('mycrazymodule '.$getstart-name.' successfully deployed' );
//Get id
// Clear the cache
ctools_object_cache_clear('getting_started', $form_state['cache name']);
$form_state['redirect'] = 'hellocowboy';
}
/**
* Callback for the proceed step
*
*/
function getting_started_add_subtask_next(&$form_state) {
dpm($form_state);
$getstart = &$form_state['getting_started_obj'];
$cache = ctools_object_cache_set('getting_started', $form_state['cache name'], $getstart);
}
/*----PART 4 CTOOLS FORM STORAGE HANDLERS -- these usually don't have to be very unique ---------------------- */
/**
* Remove an item from the object cache.
*/
function getting_started_clear_page_cache($name) {
ctools_object_cache_clear('getting_started', $name);
}
/**
* Get the cached changes to a given task handler.
*/
function getting_started_get_page_cache($name) {
$cache = ctools_object_cache_get('getting_started', $name);
return $cache;
}
//Salesforce Functions
function send_first_form($lastname, $firstname,$email,$phone, $newsletter){
$send = array("LastName" = $lastname , "FirstName" = $firstname, "Email" = $email ,"Phone" = $phone , "Newsletter__c" =$newsletter );
$sf = salesforce_api_connect();
$response = $sf-client-create(array($send), 'Lead');
dpm($response);
return $response-id;
}
function send_second_form($workshop, $attendees, $leadid){
$send = array("Id" = $leadid , "Number_Of_Pepole__c" = "2" );
$sf = salesforce_api_connect();
$response = $sf-client-update(array($send), 'Lead');
dpm($response, 'the final response');
return $response-id;
}