Hello everyone,
I'm new to Drupal but familiar with PHP. This is my first assignment as an intern and I spend too much time stuck at it so I extremely appreciate every second of your time and any thought that you guys might have.
We're supposed to make a "Form that makes forms" where admin would specify the fields that he need and our module would generate that form for him.
I created this form. on submit it would create a node where I put in a static form just to try. Unfortunately, The newly created node always comes up as a blank page with the title that I had specified.
Below is the code of the module, I made desperate attempts to add any content to the newly created node and nothing shows up. Upon create of the node used dsm($node) and I can see the content that I had added to it. But on the actual node there is nothing.
function custom_enrollement_menu(){
$items = array();
$items['admin/content/boogla'] = array (
'title' => 'builder' ,
'description' => 'subscription_form_builder',
'page callback' => 'drupal_get_form',
'access arguments' => array('access content'),
'page arguments' => array('custom_enrollement_form'),
// 'file' => 'form.inc',
'type' => MENU_CALLBACK ,
);
return $items ;
}
/*
* Page will return this form where admin will specify the fields required
*
* */
function custom_enrollement_form($form,$form_state)
{
$form = array();
$form['Welome'] = array(
'#description' => t('Welcome'),
'#type' => 'markup',
'#markup' => '<p style="text-align:center"><b>Welcome to the form builder, please provide requested information</b></p>',
'#weight' => -10,
);
$form['title'] = array(
'#required' => '1',
'#description' => t('Please provide the title of your form'),
'#weight' => '1',
'#type' => 'textfield',
'#title' => t('Title'),
);
$form['logo'] = array(
'#weight' => '2',
'#description' => t('Please provide logo of your organisation.'),
'#required' => '0',
'#type' => 'file',
'#size' => '30',
'#title' => t('Logo'),
);
$form['fields'] = array(
'#weight' => '4',
'#required' => '0',
'#key_type_toggled' => '0',
'#description' => t('Please check the fields requested in your form.'),
'#type' => 'checkboxes',
'#options' => array(
'first_name' => t('First name'),
'last_name' => t('Last name'),
'Package' =>t('Package'),
'Adress' => t('Adress'),
'City' => t('City'),
'State' => t('State'),
'Phone' => t('Phone'),
'Email' => t('Email'),
'Gender' => t('Gender'),
'Birth_Date' => t('Birth date'),
'Payement_info' => t('Payment Information'),
),
'#default_value' => array(
'0' => 'one',
'1' => 'two',
),
'#title' => t('Requested fields'),
);
$form['button'] = array(
'#value'=>'Generate Your Form',
'#type'=>'submit',
'#submit'=>array('create_custom_form_submit'),
'#weight' => 10,
);
return $form;
}
/*
* The following will generate a new node containing the newly requested form
* */
function create_custom_form_submit($form,&$form_state)
{ dsm($form);
dsm($form_state);
//will contain information submite on requested form
$values = array() ;
$values['title'] = $form_state['values']['title'];
//going through the checked fields setting 0 if field is not requested, field if it is.
foreach($form_state['values']['fields'] as $key => $value) {
$values[$value] = $value ;
}
//new node where we will put in the new form
$node= new stdClass();
$node->type = 'form' ;
$node->title ='Your New Customized Form Entitled : '.$values['title'] ;
node_object_prepare($node);
$content = node_view($node, true);
$node->field_montitre['en'][0]['value'] = $values['title'] ; //und is the language
//as in enrollement
$node->language = en ; // Or e.g. 'en' if locale is enabled
$node->status = 1; //(1 or 0): published or not
$node->promote = 0; //(1 or 0): promoted to front page
$node->comment = 0; // 0 = comments disabled, 1 = read only, 2 = read/write
$node->translate = 1;
//Trying to add content to the newly created node
$node->body = 'ok ok' ;
$node->content['body']['#value']= 'ok not ok';
//adding the 'example form' the the newly made node
$form = drupal_get_form('create_custom_example_form');
$node->content['create_custom_example_form'] = array('#markup' => drupal_render($form), '#weight'=>-10,);
// webform_node_view($node,'full');
// print theme_webform_view($node->content);
field_attach_prepare_view('node', array($node->nid => $node), 'full', 'en');
entity_prepare_view('node', array($node->nid => $node), 'en');
$node->content += field_attach_view('node', $node, 'full', 'en');
$node = node_submit($node); //perpares node for saving
node_save($node); // Save the node
drupal_set_message('Your node created form custom form!','status');
dsm($node);
Drupal_goto('/node/'.$node->nid);
return ($form);
}
/*
* An example form to be added to the new node
* */
function create_custom_example_form($form, &$form_state)
{
// now I add a text field to the form
// with a label and fixed dimensions (you never know...)
$form['first_name'] = array
(
'#title' => t('First Name'),
'#type' => 'textfield',
'#size' => 32,
'#maxlength' => 128,
);
//// now I add also a button
//$form['submit'] = array
//(
//'#type' => 'submit',
//'#value' => t('Submit'),
//);
//$form['#submit'][] = 'example_submit_handler';
return $form;
}
Thank you very much for your help. I really tried everything out there and I would appreciate any advice.
Comments
already been done ...
The form builder module already does all of what you want, and much more.
https://www.drupal.org/project/form_builder
spritefully yours
Technical assistance provided to the Drupal community on my own time ...
Thank yous appreciated ...
How would it fit?
Thank you for a quick answer.
I have seen the form builder module. I wish I could work with that module, However my form builder needs to be automatically linked to a back-end API (an enrollment API), since the users of the form builder have no advanced technical knowledge. they need to provide their customers with a working form that allows them to get enrolled.