0

What I need to do is save a node and then add another node that uses some of the data in the saved node. I have figured out how to add another button onto the form that will save the node first and then do something else.

In my form alter I have added a second submit button to the original form:

   // Check for a particular content type's node form.
   if (($form_id == 'content_node_form')) {

   $form['actions']['submit_another'] = array(
   '#type' => 'submit',
   '#name' => 'save_another',
   '#value' => t('Save, and Add Another Submission.'),
   '#submit' => array('node_form_submit','create_partial_duplicate_handler'),
   '#weight' => 20,  
   );

   }  //endif

It saves the node then, runs another function that will load some information from the just saved node into a new node.

Should the create_partial_duplicate_handler() function go to a menu item that calls another function or could I load the node form and the values directly in the function?

I have been reading lots of examples that create a menu item with a different url, but have not seen it done any other way. I don't want to waste time trying another method if the menu item method is the only way to do it.

Please let me know. Thanks.

ETA: I am using a second button because the user will have the choice of saving the node with the information entered into the form or saving the node and having another form appear with partial data from the saved node. The user enters the rest of the information into the partially filled form and again has the option of saving or saving and adding another.

0

I think there are no need of any menu or other submit button.

You can use hook_node_insert for create a different node form partial data of a node upon creation on node.

The statement is like:

function MODULENAME_node_insert($old_node) {

 if($old_node->type === 'article'){ // check node type

    $new_node_title =  $old_node->title; // get partial data from old node

    $node = new stdClass(); // We create a new node object
    $node->type = "page"; // Or any other content type you want

    $node->title = $new_node_title; // Assign partial data of old node to new node

    $node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
    $node->path = array('alias' => 'your node path'); // Setting a node path
    node_object_prepare($node); // Set some default values.
    $node->uid = 1; // Or any id you wish
    // Let's add standard body field
    $node->body[$node->language][0]['value'] = 'This is a body text';
    $node->body[$node->language][0]['summary'] = 'Here goes a summary';
    $node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field

    $node = node_submit($node); // Prepare node for a submit
    node_save($node); // After this call we'll get a nid
 }

}

If this not fullfil your requirement then use some other HOOKS provided by Node API

  • I am using a second button because the user will have the choice of saving the node with the information entered into the form or saving the node and having another form appear with partial data from the saved node. The user enters the rest of the information into the partially filled form and again has the option of saving or saving and adding another. The method you describe above, creates the node immediately which is not the behaviour I need. – Venn Apr 3 '14 at 16:20
  • @Venn Edit your question and add this info in it. – Rupesh Apr 3 '14 at 17:15

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.