Drupal 7. Custom module. I have a content type 'RFI' with a handful of fields I created with Drupal UI. I want a user to: fill out a form & click submit while drupal: saves fields to database, creates a new node of 'RFI' and populates the fields of the node with the content provided by the form's corresponding fields.
From what I understand:
Build a form with fields (taking from the 'DBTNG examples' module just for clarity):
function rfi_build_form($form, &$form_state) {
$form = array();
$form['add'] = array(
'#type' => 'fieldset',
'#title' => t('Add a person entry'),
);
$form['add']['name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#size' => 15,
);
$form['add']['surname'] = array(
'#type' => 'textfield',
'#title' => t('Surname'),
'#size' => 15,
);
$form['add']['age'] = array(
'#type' => 'textfield',
'#title' => t('Age'),
'#size' => 5,
'#description' => t("Values greater than 127 will cause an exception. Try it - it's a great example why exception handling is needed with DTBNG."),
);
$form['add']['submit'] = array(
'#type' => 'submit',
'#value' => t('Add'),
);
return $form;
}
Here the form fields are saved to database with database API.
function rfi_build_submit($form, &$form_state) {
global $user;
// Save the submitted entry.
$entry = array(
'name' => $form_state['values']['name'],
'surname' => $form_state['values']['surname'],
'age' => $form_state['values']['age'],
'uid' => $user->uid,
);
$return = dbtng_example_entry_insert($entry);
if ($return) {
drupal_set_message(t("Created entry @entry", array('@entry' => print_r($entry, TRUE))));
}
}
Now here is how I know how to create a node of RFI type. How do I take the values of the fields above [name], [surname], [age] and have them related to corresponding fields in my content type?
function rfi_build_type($form, &$form_state){
$node = new stdClass();
$node->type = 'rfi';
$node->name = '';
$node->surname = '';
$node->age = '';
$node->language = 'en';
$node->uid = 1;
node_save($node);
}
I was thinking that since the fields have been saved already, that I could use a database query to get these values, but intuition tells me I'm overlooking something.
Thank you!