Question

I want to add a button "Save and add more" to node add form drupal 6. on click on this button page should redirect to same node add form after saving node. I have a content type child to add Child, user may have more than one child so if he/she want to add another child he/she will click on "Save and add more" and user have only one child he/she will click on "Save" so basically only redirection is to be change for new button.

Was it helpful?

Solution

You'll need to create a simple module containing two files to do this. Create a new directory "addmore" in your /sites/all/modules/custom folder (create that folder if it doesn't exist) and create the following files in that directory:

  • addmore.info
  • addmore.module

Contents of addmore.info:

name = "Add More"
description = Create and Add More button on Child node forms
package = Other
core = 6.x

Contents of addmore.module (Assumes that the machine name of the "Child" content type is "child")

<?php
/**
 * Implementation of HOOK_form_alter
 *
 * For 'child' node forms, add a new button to the bottons array that submits
 * and saves the node, but redirects to the node/add/child form instead of to
 * the newly created node.
 */
function addmore_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'child_node_form') {
    $form['buttons']['save_and_add_more'] = array(
      '#type' => 'submit',
      '#value' => t('Save and add more'),
      '#weight' => 20,
      '#submit' => array(
        // Adds the existing form submit function from the regular "Save" button
        $form['buttons']['submit']['#submit'][0],
        // Add our custom function which will redirect to the node/add/child form
        'addmore_add_another',
      ),
    );
  }
}

/**
 * Custom function called when a user saves a "child" node using the "Save and
 * add more" button. Directs users to node/add/child instead of the newly
 * created node.
 */
function addmore_add_another() {
  drupal_goto('node/add/child');
}

Once you have created these files, navigate to your modules page and enable the "Add More" module.

That's it. You'll see a new "Save and add more" button which does just that on Child node creation and edit forms.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top