1

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!

1

You seem to be going in a slightly wrong direction with this (until I'm missing or not understanding something). If your goal is to create a node based on the data users send via a custom form then it can be done in the form submit handler. Fields added via the GUI don't need separate handling. For example if you are using node_save() then adding a custom field value could look similar to this:

$node->language = LANGUAGE_NONE; // Or e.g. 'en' if locale is enabled.
$node->field_name[$node->language][0]['value'] = $form_state['values']['name'];

and with drupal_form_submit():

$new_form_state['values']['field_name'][$node->language][0]['value'] = $form_state['values']['name'];

There's a solid amount of information available about this on the web that you might want to check out for further assistance (like this, this and this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.