1

We have a meeting room booking system that needs one extra feature added. Meeting rooms 1 and 2 have a removable wall so people can book Room 1, Room 2 or Rooms 1 and 2. What I need is a way during booking for the option to book one or both rooms.

My plan: Offering separate booking forms for Room 1 and Room 2 and just have a field where they can check off if they need the other room booked with it. If this box is checked, it will create a duplicate booking with a new ID and the room number (Term reference list) changed.

From my reading, I think I need to use node_save() to do this? I am not sure where to call this from though. I assume I need to call it from one of the hook_node_* things (as we already do for some other custom code)?

Basically, how do I create a duplicate node when submitting a form with almost identical data but one field is changed before it saves the copy?

Thanks!

0

2 Answers 2

0

Drupal core doesn't provide hook after a node has been saved, but you can use hook_node_presave() like

<?php
function mymodule_node_presave() {
  $var = $node->original->field_my_field['und'][0]['value']; 
  // field_my_field could be the checkbox value, if both rooms should be booked
  if ($var == true) { make_a_copy_but_change_a_value(); }
}
?>

Using this hook, you can look for your extra checkbox value (if the second room should be booked, too) and create a copy of the node object. But beware, at this point (hook_node_presave) the original node is not yet fully written to the database. However, the cool thing is that it fires both on node save and node update, so if you need to do something both on the creation and the updating of a node (e.g. if a booking of both rooms changes).

To actually use a hook after node insertion, you may want to try the Hook Post Action module as it provides special hooks. Drupal core does not provide such hooks, hook_node_insert and hook_node_update are both fired after the sql query for insertion is scheduled for execution but due to the transactional system the data is not yet in the database.

3
  • Thanks to both of you for suggesting node_presave as the way to go. Now I have one more really dumb question. When using node_save, it appears I need to set each field on the duplicate node manually. A few questions about that: 1. Can I do this with some kind of loop and just set the few things I want changed at the end? 2. If not, which fields do I need to manually set? Taking a peek at the $node using dpm(); I am see a ton of variables. Are most of those set automatically? Or do I need to copy every single thing?
    – Comprox
    Commented Sep 30, 2014 at 20:21
  • Please if you can paste the dpm() of $node here.
    – Xeeshan
    Commented Oct 1, 2014 at 8:06
  • Sure, here it is on pastebin since it is too long to paste here. This paste is also only the top level. Some of these fields contain many sub fields (such as rdf_mapping). pastebin.com/HEB9VKFY
    – Comprox
    Commented Oct 1, 2014 at 16:32
0

You can use the hook_node_presave with node->is_new.

<?php
  function yourmodule_node_presave() {
    if (isset($node->is_new)) { 
     // If node is inserted  first time
      $room_2 = $node->field_my_field['und'][0]['value']; //Get the checkbox value
      if ($room_2 == true) {
       //create new node
       }
    }
  else{
  // Perform your action if node is updated as you should not create duplicate
  //room2-node at room-1 node update. 
  }
  }
   ?>

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.