In Drupal 7, I am trying to programmatically create a custom content type. I have defined my custom content type implementing hook_node_info().

For my node editing form, I am using hook_form() and defined my custom node's title here. I used hook_form_alter() to convert my node editing form into a multi-step form, where I used the following code.

if ($step == 4) {
  $form['actions']['finish'] = array(
    '#type'   => 'submit',
    #value'  => t('Submit'),
// Add validation handler.
$form['#validate'][] = 'mymodule_form_validate';
// Add submission handler.
$form['#submit'][] = 'mymodule_form_submit';   

function mymodule_form_validate($form, &$form_state) {
  drupal_set_message(t('validating multistep form'));

function mymodule_form_submit($form, &$form_state) {
  switch($form_state['triggering_element']['#value']) {
    case $form['actions']['next']['#value']: // if 'Continue >>' is clicked.
      $form_state['rebuild'] = TRUE;
    case $form['actions']['finish']['#value']: // if 'Submit' is clicked.
      $form_state['rebuild'] = FALSE;
      // To Do.
      // Store the values from the form in the database here.
      drupal_set_message(t('Your form has been submitted'));

My multi-step form is working great, but I am stuck on my last step of the form, where I save the user submitted values in the database. I am not getting any reference on this topic. I am looking for information on how to save my this node in the database, what hooks to use, and some more details. Please guide me in the right direction.


After you have constructed your custom node object by stepping through your multi-form, use the node_save() function. That will write your node to the database.

To see how Drupal creates and saves it nodes you can take a look at node_add(). Hope that helps a little.

| improve this answer | |

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.