Creating nodes depending on terms added to user

Events happening in the community are now at Drupal community events on www.drupal.org.
Letharion's picture

Hello Rules fans :)

I'm giving Rules my first serious try, and hoping someone will be able to provide some guidance.

What I want to do:
Users may add terms to themselves. For every available term, there is a CCK type. The type has a user reference.
When term "X" is added to user Y, I want to create a node of type "X-node", and set the user reference to user Y.

My first stumbling block is this:
When user information is saved, how do I know which terms are new, and thus do not already have a corresponding node created?
I see there's a trigger for "User account details have been updated". If there was also a trigger like "User account details will be saved", then perhaps I could load the terms before save, and then compare with the new data?

Comments

Pretty tricky

itangalo's picture

Hello future Rules fan! (Or are you already convinced?) :-)

Just to be sure, I try to rephrase your question.

  • You have a vocabulary with a limited number of terms, say alpha, beta and gamma.
  • You also have a number of node types, one for each term above (hereby called alpha-nodes, beta-nodes and gamma-nodes).
  • Each user may add any of the terms above to their account (not Content Profile?).
  • You want use Rules to make sure that each user has nodes corresponding to every term his/her account is tagged with. (That is, if user account has terms alpha and gamma, there should be a alpha-node and a gamma-node.) Also, these nodes should have a user reference back to the user.

Ok.

I'm pretty sure what you're after can be done with Rules and Views, if you allow a PHP snippet or two. On the other hand, it also seems like you're creating a pretty complex structure for something that probably could be easier. (But without further information about how you want to use the nodes it is pretty difficult to say.)

With all that said, here's a sketch for how you could do it all:

  1. Create a View that takes arguments to list all nodes belonging to a particular user, of a particular type. ("All gamma-nodes for user 12", where gamma and 12 are decided by arguments.)
  2. On user account save, run custom PHP to execute the view for each term.
  3. Check the view results (also programmatically) to see if there are any nodes created already. If not, run an action to create a new node.
  4. You will probably have to use a rule set to be able to save the new node, and in a follow-up rule set the user reference field. (I think, but am not sure, that the node has to be saved before you can change CCK values.)

I recommend that you put everything in a rule set, and make one rule for each term you want to check. (And then a triggered rule that calls the rule set.)
The most tricky part will probably be the run-views-and-use-results-as-conditions thing. I don't know the API for Views enough to supply a PHP snippet here, but I'm pretty sure that if you do it correctly it shouldn't be more than five lines of code for loading the view, executing it and checking the results. But then you have to trigger further stuff based on the outcome. (Perhaps you can put it all in a rules condition, which would be pretty neat.)

I have two suggestions for how this could be simplified:

Suggestion one: Use Content Profile to manage the taxonomy terms. This will make it possible to use the action 'content is going to be saved' trigger, and compare before and after states. Then you can get rid of the view stuff, and instead focus on comparing the saved and updated node.

Suggestion two: Consider changing the user reference field to simply the ownership (author) of the node. Then you can skip the user reference part of the actions. (But if you want to keep the ownership separate, eg for permission reasons, a user reference field will be fine.)

You're not choosing the easiest task to start with, so I'll give you all the good luck I can muster!
//Johan Falk, NodeOne, Sweden

Great information! The

Letharion's picture

Great information!

The "Content Profile ... will make it possible to use the action 'content is going to be saved' trigger" was precisely the information I was looking for. I seem to recall that you used that when showing Rules on DrupalCon. (See, a fan already) I'm gonna try going that route, and hopefully I can skip the view.

Regarding "complex structure", I'd be happy to get a different suggestion on how to implement it. (A.k.a how not to (ab)use Rules). This post reflects rather well what we're trying to accomplish
http://commons.acquia.com/discussion/adding-questionnaire-specific-user

After trying Content Profile, I'm not entirely sure how it will help me. Updating the terms on a user only seems to trigger the "User account details have been updated". Any idea what else I will need to be able to use the content triggers? Perhaps the tags need to be added someplace else...

Meanwhile I'm gonna try working with the view like you suggested first, and perhaps take a look at the CP version again when I've cleared my head.

Thanks a lot, for the info and the luck.

Terms on the Content Profile!

itangalo's picture

Put the terms on the content profile node, instead of on the user accout. Then you can trigger on the node update event, rather than the account saved event. (And you don't have to mess with core's Profile module, which has seen better days.)

Good luck!
//Johan Falk, NodeOne, Sweden

Rules

Group organizers

Group categories

Categories

Group notifications

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

Hot content this week