Copy taxonomy terms from nodes to CCK-nodereferenced node.

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
sjf's picture

With Rules, is it possible to copy taxonomy terms from nodes X, Y and Z to node N? Nodes X, Y and Z would contain a CCK nodereference field to reference node N.

Comments

You should be able to create

caschbre's picture

You should be able to create a rule that triggers when a node is created and then checks to see if the referenced node has taxonomy, then adds those (referenced node) terms to the new node. To do this though will probably require the action to be PHP and I'm not really sure what the PHP would look like.

Thanks for the quick

sjf's picture

Thanks for the quick response. I actually want to add terms to node N (the referenced, pre-existing node) upon creation of nodes which reference it (or at cron time), but I suspect that the answer is the same and I will need a PHP action. I'm not sure what it would look like either.

Ah, sorry I misread the

caschbre's picture

Ah, sorry I misread the original post.

That may be a bit more difficult. With rules you'd have to load the node being referenced, add the terms, and then save it. Check the rulesets to see if you can pass in a referenced node. If you can with rulesets then it should be a bit easier. If not, you may have better luck with the cron route.

I have got as far as creating

sjf's picture

I have got as far as creating a ruleset with actions: load a referenced node, load a vocabulary and assign term to content (referenced). Now I want to schedule that rule so I go to Triggered Rules and try to add a rule to run on the event of cron. However, when I try to add an action: Rule Scheduler to schedule my rule set, I find this isn't possible due to unavailable arguments.

I can solve this by loading a vocabulary and term again in the new rule, but I have to choose a specific term rather than being able to select the terms of the referencing node. I guess this is where the php action comes in.

Can you trigger on the node

caschbre's picture

Can you trigger on the node being updated / saved and then call your ruleset? I wouldn't worry about scheduling it to cron just yet.

I created a triggered rule

sjf's picture

I created a triggered rule with event:after saving new content. I then replicated my actions: load referenced node, load vocabulary, load term (this time it asked for a specific term - I chose Apples as a test), and finally modify node taxonomy term (set to referenced node and again, the term set to Apples).

I tried creating a new node X, referencing my node N, and saved it. When I checked node N, the term Apples had not been added. Have I missed a step?

i need this feature too. was

bakr's picture

i need this feature too.

was wondering about the use of "Load ..." actions, you have given me an insight.

Alternatively, I am following another route:

I am building "relationships" in "views" to create a flat filterable report, that show parent/child nodes.

keeping in mind that the child is using cck-taxonomy field.

Subscribing, greetings,

Summit's picture

Subscribing, greetings, Martijn

Now available as feature and Rules import

Itangalo's picture

This was an interesting problem, so I wrapped up an installable feature that shows an example of how it can be solved. (The rule settings are also available as importable code.)

What makes this problem difficult is, I think, two things:

  1. Rules (and many other modules) has problems with handling multiple-value fields, such as taxonomy tags.
  2. The rules action for modifying taxonomy terms doesn't take Tokens, and it doesn't accept PHP.

I circumvented this by using a small PHP script:

<?php
// Loop through all taxonomy terms in the source node
foreach ($node->taxonomy as $tid => $termdata) {

 
// If the term isn't already present in target node, add it
 
if (!isset($referenced_node->taxonomy[$tid])) {
   
$referenced_node->taxonomy[$tid] = $termdata;
  }
}

// Tell Rules to save the variable $referenced_node
return array("referenced_node" => $referenced_node);
?>

It's all downloadable in the Rules documentation: http://drupal.org/node/817422

Good luck!
//Johan Falk, NodeOne, Sweden

Follow up question

Itangalo's picture

I got this question in the mail box, and copied it here to make the answer more visible.

[…]

I am trying to modify it so it only adds taxonomy terms from certain vocabularies, but every time I modify your code and paste it in for import, it gives me an "Import failed" error message. Here's where I'm at so far:

foreach ($node->taxonomy as $tid => $termdata) {
   // If the term isn\'t already present in target node, add it
   if (!isset($referenced_node->taxonomy[$tid])) {
     // If term belongs to vocabulary 1 or 3
     if (($termdata->vid=='1')||($termdata->vid=='3')) {
            $referenced_node->taxonomy[$tid] = $termdata;
     }
   }
}

Why oh why would this cause it not to import? I can't see a way to edit your PHP code after importing, so this is quite frustrating.

Thanks!

Heather Ann

P.S. Love the videos on the NodeOne blogg! So useful!

Strange

Itangalo's picture

It's strange that the import won't work when you have edited it, and I can't see anything wrong in your code. It could possibly be some kind of formatting errors, if your editor handles line breaks or something differently than the rest of the files in the feature (but that seems far-fetched).

However, it should be very much possible to edit the PHP even after activating the feature. One of the actions (I can't remember what I called it at the moment) is a 'execute custom PHP' action, and that action has a field with all the PHP included and editable. Take a look at both triggered rules and any rule sets, and you will eventually find the action.

If the feature itself prevents you from changing the rules (which could be the case, I'm not sure), you can clone the rule and then make changes in the copy.

Good luck!
//Johan Falk, NodeOne, Sweden

PS: Thanks for your question, and thanks for the encouraging feedback on the screencasts!

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: