Remove a term from content is not working. Not sure why.

Tested with: 6.x-1.1 and 6.x-1.x-dev
Tested with patch: [456328#comment-1639754]

My workflow is:
Load a vocabulary
Load a term
Remove a term from content

PS. I can assign term to content with no problem:
Load a vocabulary
Load a term
Assign a term to content

Comments

klausi’s picture

Component: Forms Support » Rules Engine

Does not seem to be Rules Forms related.

adpo’s picture

Indeed, It is not about Rules Forms but Triggered rules. Does anyone had a similar issue with rules and found an answer? Thank you for reply.

scholesy’s picture

what I did was make a call to:

taxonomy_node_delete(..)

in function rules_action_taxonomy_term_remove_from_content(..) in taxonomy.rules.inc instead of all the things that the function is originally doing, and that seems to fix the problem.
hope this helps.

adpo’s picture

Thank you I will try

costas vassilakis’s picture

Hi scholesy ,
I have the same problem,
How did you make this call (taxonomy_node_delete(..))
Can you provide a more detailed code?

scholesy’s picture

OK, what I did was just replace what was inside function rules_action_taxonomy_term_remove_from_content, so the function now is simply:

/**
* Action: Remove a term from content.
*/
function rules_action_taxonomy_term_remove_from_content($node, $taxonomy_term, $settings) {
taxonomy_node_delete($node);
}

Hope this helps.

kunago’s picture

Version: 6.x-1.x-dev » 6.x-1.3
Status: Active » Needs review
StatusFileSize
new1.51 KB

I cannot agree with the fix posted in #6 as this would remove all the associations from a node. All that is desired is to remove one single term.
I created this patch that resolves it for me. A little bit of explanation for the code.

The taxonomy module in Rules assumes that the taxonomy terms are stored in $node->taxonomy[$taxonomy_term->tid]. Experimentally I found it this is not true. If there is one vocabulary assigned to a content type, the path would be $node->taxonomy['delta'] => $taxonomy->tid. If there are more than one vocabularies, the path is $node->taxonomy['taxonomy-field-number'][$taxonomy->tid] => $taxonomy->tid. I hope I got this right.

Anyway, in the patch I am using a recursive search in array function that browses $node->taxonomy and tries to find the $taxonomy->tid in it. If it does find it, it then based on how deep the term is, it unsets the variable and therefore removes the term from the node.

The code is running on my site and does what is expected from it. I am not familiar with the way of adding new functions to the code, what is the way to create the functions' names, variables, etc. So please do make the changes to the code so that it respects the Rules module's coding.

kunago’s picture

StatusFileSize
new1.46 KB

Resubmitting the patch with Unix-style line endings.

kunago’s picture

StatusFileSize
new1.49 KB

New patch submit for testing.

Status: Needs review » Needs work

The last submitted patch, taxonomy.rules_.patch, failed testing.

klausi’s picture

This patch contains absolute paths, make sure that you create the patch from the module folder which should give relative paths.

kunago’s picture

Will this do?

--- modules/rules/rules/modules/taxonomy.rules.inc	Wed Aug 05 19:20:41 2009
+++ modules/rules/rules/modules/taxonomy.rules.inc	Sun Sep 19 20:19:45 2010

Sorry, but I am using WinMerge to create the patch and don't know how to tweak the patch file so it passes through the testing procedure.

klausi’s picture

kunago’s picture

StatusFileSize
new1.45 KB

I followed the guide and don't know why the patches don't validate...
So one more try.

EDIT: This time the patch is being ignored by Simpletest. Any volunteers willing to test, feel free.

klausi’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, taxonomy.rules_.inc_.patch, failed testing.

melchoir55’s picture

subscribe

melchoir55’s picture

Attempted to apply your patch. Cygwin just hung up when it was ordered to apply it. I'm guessing it isn't compiled properly.

edit:
Manually applied patch. Appears to be functioning as intended.

I recommend fixing the patch such that it can be added.

kunago’s picture

Creating such a patch will need to be the task for someone who knows how because I tried several times following step-by-step manuals which never made it work. For some reason I am unable to create the patch on Windows with WinMerge.

lucio.ferrari’s picture

I humbly disagree with post #7, I tried to implement the patched function via custom php action and it didn't work.
"The taxonomy module in Rules assumes that the taxonomy terms are stored in $node->taxonomy[$taxonomy_term->tid]", and it does indeed, in fact by using the standard code for the function:

$node = node_load(123); //some nid
$tid = 20 //the tid to be removed

if (isset($node->taxonomy[$tid])) {
   unset($node->taxonomy[$tid]);
}
node_save($node);

in an 'execute PHP' block, the taxonomy term is removed.
I think the problem is quite deeper, maybe it depends on when the function is called, though I tried to trigger the rule when 'content is going to be saved' AND 'after updating/saving content'. Neither works.

lucio.ferrari’s picture

Whoops, I guess I didn't implement the function correctly, it does work, my kudos!

To use it as an 'execute custom php' - since I'm not so keen to directly modify modules - i rewrote it as:

$recursive_search = _dummy_action_taxonomy_term_remove_from_content_helper($term_pp->tid, $node->taxonomy);
if ($recursive_search != FALSE) {
	switch (count($recursive_search)) {
		case 2:
			unset($node->taxonomy[$recursive_search[0]][$recursive_search[1]]);
			break;
		case 1:
			unset($node->taxonomy[$recursive_search[0]]);
			break;
		default:
			break;
		}
	unset($recursive_search);
	return array('node' => $node);
	}
	

/**
* Helper: This is a helper function for recursive array browsing
*/

function _dummy_action_taxonomy_term_remove_from_content_helper($needle, $haystack, $path=array()) {
	if(!is_array($haystack)) {return FALSE;}
		foreach($haystack as $key => $val) {
			if(is_array($val) && $subPath = _dummy_action_taxonomy_term_remove_from_content_helper($needle, $val, $path)) {
				$path = array_merge($path, array($key), $subPath);
				return $path;
			} 
			elseif($val == $needle) {
				$path[] = $key;
			return $path;
			}
		}
	return FALSE;
	}

Incidentally, the action 'assing term to content' is equally flawed: it tries to assign the term even when the term is already assigned. This results in a SQL error, since the table entry in 'node_term' already exists.

The problem is the same, because the if(!isset ... ) check doesn't find the right value in the array, it assumes it doesn't exists and proceeds to duplicate it.

This means - as kunago correctly pointed out - that the $node object which is passed to the action is not the same you get with node_load($nid);, in which the terms are objects nested into the $node->taxonomy array.

In post #7 kunago says that it is instead in

$node->taxonomy['taxonomy-field-number'][$taxonomy->tid] => $taxonomy->tid

What is 'taxonomy-field-number'...?

lucio.ferrari’s picture

Status: Needs work » Closed (fixed)

Latest release fixed the problem.