6

I added the following code in hook_form_alter() and I get my button on the Add/Edit node form:

$form['actions']['save_add_lesson'] = array(
  '#type' => 'submit',
  '#value' => 'Save & Add a Lesson',
  '#name' => 'op',
  '#submit' => array('mymodule_node_form_submit'), /* NEED this */
  '#weight' => 1000
);

But, clicking the button doesn't seem to hit hook_node_submit() or hook_node_insert(). I'm assuming I'm missing a hook or something in the button creation...anyone have any ideas?


Then added this function:

function mymodule_node_form_submit($form, &$form_state) {
  // Do stuff before saving the node

  // Call this to save the node
  node_form_submit($form, $form_state);

  // Do stuff after saving the node
}
2
  • Try making it a submit instead of a button, that normally does the trick for me
    – Clive
    Commented May 29, 2012 at 13:56
  • Sounded like it would work, but it didn't :( I updated the code above to reflect the changes... Commented May 29, 2012 at 14:25

3 Answers 3

7

Got it :D

Adding

'#submit' => array('mymodule_course_form_submit'),

To the submit button worked. Like this:

$form['actions']['save_add_lesson'] = array(
  '#type' => 'submit',
  '#value' => 'Save & Create a Lesson',
  '#name' => 'op',
  '#submit' => array('mymodule_course_form_submit'),
  '#redirect' => 'node/add/lesson',
  '#weight' => 1000
);

function mymodule_course_form_submit($form, &$form_state) {
    $nid = mymodule_node_save_no_redirect($form, $form_state);  

    $form_state['redirect'] = 'node/add/lesson';
    unset($_GET['destination']);
    drupal_static_reset('drupal_get_destination');

    if($nid != -1) {
        $_SESSION['mymodule_course_nid'] = $nid;
        drupal_redirect_form($form_state);      
    } else {
        // Failed to save Course...notify user
        drupal_set_message('Failed to save Course Content', 'error');
    }
}   
1
  • 1
    works like a charm, but an interesting part is missing here mymodule_node_save_no_redirect, i used these two lines for that $node = node_form_submit_build_node($form, $form_state); node_save($node);
    – rémy
    Commented Oct 27, 2013 at 11:05
0

There is also a good Content Type: Extras module for that + many more options:

  • Set default values for common content type settings (see below) - you can still override the defaults per content type

    'Save and New' button to add new nodes of the same type right after saving

    'Save and Edit' button to save a node and continue working on it (great for long posts so you don't have to worry about losing all of your information).

    User permissions for content type duplicated on content type add/edit page

    Sets a maximum length (26 characters) for 'Add new field' and 'Add new group' (if field_group module is enabled) machine names to prevent submitting the form and getting an error message

    Add a 'Save' button to the top of the 'manage fields' form

    Auto-populate new field machine name, new group machine name, and existing field label on manage field forms

    Add a 'Cancel' button to node edit forms

    Ability to set node title visibility when viewing node

    Ability to hide title of front page (takes precedence over content type setting)

    Ability to add form submission buttons to top of 'Manage fields' content type forms and/or node edit forms.

    Ability to remove body field on content type creation.

    Ability to set description field in content type edit form to required.

Set defaults for:

  • Title field Preview button

    'Save and New' button (with configurable text)

    'Save and Edit' button (with configurable text)

    Published

    Promoted to front page

    Sticky at top of lists

    Create new revision

    Display author and date information

    User permissions

    Node title visibility per content type

1
  • I saw this, but there is some extra workflow stuff that I need beyond simply Saving and creating a new node ;) Commented May 30, 2012 at 13:24
0

The following code worked for me to add a second button on a form that saves and creates another item, instead of going straight to see the new node.

function MYMODULE_MYFORMID_form_alter(&$form, &$form_state, $form_id){
  $form['actions']['submit2'] = array(
    '#type' => 'submit',
    '#value' => t('Save & Create Another Item'),
    '#access' => TRUE,
    '#submit' => array('MYMODULE_node_form_submit'),
    '#redirect' => 'node/add/new-node-item',
    '#weight' => -30,
  );
}

function MYMODULE_node_form_submit($form, &$form_state) {
 if ($form_state['nid']) {
   $form_state['#redirect'] = 'node/add/new-node-item';
 }
}

See https://www.drupal.org/node/290462.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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