I have a module that implements hook_nodeapi - when certain content types are updated, other actions occur. However, I'm finding that drupal fires off these actions before actually saving the data in the node.

For instance, when someone updates a node and certain criteria is met, a script is then called to do some work in the database. When the user hits save, the script runs but does not see the updated information. If the user updates the node in a way that doesn't fire off the script, saves it, then goes back in to make the script fire off, the script then sees the previous updates.

Is there a way to force drupal to save the node before doing anything else within the update case of hook_nodeapi?

Comments

nevets’s picture

The core parts of the node are save before hook_nodeapi is called with $op equal 'update', can you be more specific on what you are doing and why you think the node is not yet saved?

fender177’s picture

I work for a university - we have an online application to apply to our program. Applicants fill out an application. If we accept the student, we fill out some information like start dates, term admitted etc. If their status is changed to accepted, a script runs in the background to take all of the information from their profile and copy it to another instance of drupal for accepted students only (there are several reasons why I chose a second installation instead of just combining them into the same drupal install).

If our student services updates the admitted term, start date etc and changes their status to accepted and then hits save, the script I wrote to copy the student info from one site to the other will copy all of the information except for what was updated before hitting save.

If I go in and set the admitted term, start date etc, hit save, then go back and change the status to accepted, the script fires off and is able to copy over all of the information.

That is why it seems like the information isn't being updated in the database before calling the script.

Here is a very stripped down version of what I'm doing:

function applicationOverride_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL){
	global $user;
	switch($op){
		case 'update':
		switch($node->type){
			case 'accel_app':	// node type
				switch($node->field_app_status[0]['value']){	// check status of application
					case 'Accepted (Active)':	
						if($node->field_app_notified[0]['value']!='Yes'){	// check if student has already been notified via email
							if($node->field_app_flashline[0]['value']!=''){
								// run script to copy applicant to portal
								exec('/usr/bin/php /var/www/copy_applicant_to_portal.php '.$node->field_app_accl_appid[0]['value'], $output, $returnvar);
								
								// check return value from script for errors
								if($returnvar!=0){
									drupal_set_message("An error has occurred while migrating the user to the student portal.  Please contact the webmaster for assistance.","error");
								}else{
									drupal_set_message("User created successfully in portal", "info");
									$result = drupal_mail('applicationOverride', 'accel_active', $node->field_app_email[0]['email'], array('language' => 'en'), $params, $from=$email, $send = TRUE);
								}	
							}else{
								drupal_set_message("There user's id was not set before changing the status.","error");
							}
						}
						break;
					}
				break;
			}
		break;
	}
}
nevets’s picture

If you add this to the top of the update code

	  	drupal_set_message("Node:<pre>" . print_r($node, TRUE) . '</pre>');

does the node show the changed values? (it does for me).

What does opy_applicant_to_portal.php do? I suspect the problem lies there, I am guessing the code there reads the data some how. Why not just make a function that includes the logic, that way you could pass it $node.

You might also find the services module useful.

fender177’s picture

It does show up... So it must be my script. It's just strange that everything else shows up, except what has been immediately changed. My script reads the information from the application node table then bootstraps the other drupal installation, creates a user for the applicant and then creates a profile (custom content type, not the typical drupal profile) node using node_save with the information grabbed from the application.

The application site and the intranet are two separate installations of drupal - both do run on the same web server though. The intranet, however, uses ldap authentication, where the application site uses regular drupal users. I think this is why I opted to create an external script rather than a new function.

Is it possible to bootstrap another drupal installation from within a function of a module? If so, how does one destroy the bootstrap after the function is finished? Perhaps there is a writeup on moving data between drupal sites?

nevets’s picture

I would look at using the services module and some custom code. Using it you could create the remote user and then create the profile from the code you have shown without a need for a bootstrap.

My suspicion is your custom script is seeing a cached copy of the node.

fender177’s picture

Very cool - I'll check that out. Your suspicion is probably correct - caching has been painful to say the least.

Thank you for all of your help!