Hi

The content types we're using allow the editor to attach taxonomy to the content.
I need to create functionality when a user is editing a node that will:

1. Check to see if the taxonomy attached to the content has changed
2. If it has, create new content instead of editing the current.

I know you can override form functions, but I'm lost on which one/ how to do this if I should be using hook_form_submit, or some other hook. Any advice is appreciated.

Thanks

James

Comments

marcvangend’s picture

I don't understand what you mean with "see if the taxonomy attached to the content has changed". Do you mean that the taxonomy term object has changed (eg. the term name or description)? Or do you mean that the terms assigned to the node may have changed meanwhile? Or do you mean that the vocabulary has changed (eg. terms have been added)? If you can explain which problem you're trying to solve and how your site should work, I can answer your question better.

jniesen’s picture

For example, we have a content type named "contact info" and a State vocab with the individual state terms. A user creates "contact info" content and selects Minnesota and Wisconsin terms when adding the content. A different user goes in, changes the content and unselects the Minnesota term. Instead of updating the content to have only the wisconsin term, I need to add a new piece of content with the Wisconsin term instead of updating the content - if that makes sense.

Basically, when a user edits a node then submits it, I need to use a hook (not sure if its form_submit or nodeapi or something else) to perform some logic to decide whether to update the node or create a new node and perform the correct operation accordingly.

kpander’s picture

If I understand you correctly, I think I'd go about it something like this...

1. Add a new 'submit' handler to the form, via hook_form_alter.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'myformid') {
    $form['#submit'][] = 'mymodule_custom_form_submit';
  }
}

2. In the new 'submit' handler, check if the taxonomy has changed.

function mymodule_custom_form_submit($form, &$form_state) {
  $nid = $form_state['values']['#node']->nid;  // Or wherever you're storing this...
  $terms = $form_state['values']['#node']->taxonomy; // Again, wherever you're storing this
  $node_old = node_load($nid);

  if ($node_old->taxonomy != $terms) {
    // Taxonomy has changed.
    // Remove $nid from node, so it'll save as a new item instead
    // of updating the existing one.
    unset($form_state['values']['#node']->nid);
  }
}

Or something like that... (the above is completely untested!)

Does that sort of sound like what you're looking for?

jniesen’s picture

Thanks,

That is what I'm looking for. I will work off that and get back to you once figured out.

vegantriathlete’s picture

If you decide to go this route, you can use hook_form_formID_alter and then you don't need to do the check for the form ID.

marcvangend’s picture

I think you don't even need to add a submit handler. You can implement hook_nodeapi (see http://api.drupal.org/api/function/hook_nodeapi/6) and do your logic when $op = 'presave'.

function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
  switch ($op) {
    case 'presave':
      if ( /*check if taxonomy has changed*/ ) {
        unset($node->nid);
      }
      break;
  }
}

As you can see in the node_save() function (http://api.drupal.org/api/function/node_save/6), the node will be inserted as new if the nid is empty. The nodeapi presave hook is invokes right before this check.

jniesen’s picture

I was able to get it working using the submit handler. However, instead of using a form id, I had to check if the form had the site taxonomy field in it (otherwise would have to enter a form_id for every content type that uses the taxonomy (also, we're using the content_taxonomy module, so the fieldname for the taxonomy is a little different:


function mymodule_form_alter(&$form, $form_state, $form_id) {
 /*
   * Looking to see if 'edit-field-site-taxonomy-value' exists.  If it does, we are
   * editing a node that has site taxonomy terms attached and need to perform some
   * custom logic found in custom_form_submit()
   */
   if (isset($form['#node']->field_site_taxonomy)) {
      $form['#submit'][] = 'custom_form_submit';
   }	

}

/**
 * Handle post-validation form submission for editing nodes with "field_site_taxonomy". 
 */
function custom_form_submit($form, &$form_state) {

	$nid = $form_state['values']['nid'];  
	$terms = $form_state['values']['field_site_taxonomy']; 
	$node_old = node_load($nid);

	if ($node_old->field_site_taxonomy != $terms) {
	   /*
     	     * Taxonomy has changed.
	     * Remove $nid from node, so it'll save as a new item instead  of updating the existing one.
	     */
	     unset($form_state['values']['nid']);	
	
	}

} 

This works great. However, it would be nice to just use the node_api hook as mentioned by marcvangend. If I get time to revisit or a chance to refactor, I will check this route out as well.

Again, Thanks for everyone's help. I