Find node after saving new user account

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

I've got a node with a user reference field (with a list of users). Desired behaviour is: when new users register themselves, add their UID to the user reference field for that single node.

So I set up a rule with an 'After saving a new user account' Event, and I assume I need an Action of type Add an Item to a List. But I can't figure out how to select the specific NID for the node I want to update with the new users, because the only items that show up in the Data Selector are Site and Account.

I'm a Rules newb - is it possible to do what I'm after? Thanks!

Comments

Fetch entity by ID

itangalo's picture

You can get the node by using the action "fetch entity by ID".

It seems a bit weird to have a node with a gazillion of users attached to it, though – like you're only using it to create a user list. If you do this with Views instead you would probably get a more robust solution. (Though I admit I don't know what you want to achieve.)

Good luck!
//Johan Falk, Sweden

Thanks Johan for the reply

jbucks's picture

Thanks Johan for the reply (your screencasts are great, by the way).

That's great - it looks like with your tip that I can at least find the node. But how do I assign the user to the user reference field on that node?

I would have thought that I would create an 'add item to list' action, but that only gives me Data Selectors related to the account.

So after the 'fetch entity by ID' action, I added a second action 'get entity by property' because at least there I can see the field I need to edit, but I can't figure out what to use for the 'The property value of the entity to be fetched' value.

It's a long story as to why I've set it up like that, it's actually not for a list of users, it's so that only users with access to a parent node can create content types which also only belong to a parent node (using Node Access Node References along with reference views). I probably should have created a reference field on the user entity which points to the node, rather than on a node which points to multiple users, but I can't redo it just yet.

Just realized

itangalo's picture

Oh, I just realized that you (as usual) will need a condition "entity has field" (or "content is of type") for the fields to appear. Without that, the user reference field won't be accessible to Rules.

This is made difficult by the fact that you can't add conditions inside the actions – and you add the node with an action. One way to work around this is to use a rule set component instead.

Sometimes things are just complex… :-/

Thanks again! After spending

jbucks's picture

Thanks again! After spending the whole day today at this, I'm definitely farther than where I was, but now stuck somewhere else.

I've got a rules component (an action set) which locates the Node ID via a 'fetch entity by ID'. I can then see the user reference field I need to access.

So back in my reaction rule, I added the component as an action, which works fine. But when I try to add a second action to assign the newly registered user's UID to the user reference field in that node, I run into trouble.

I'm assuming that the user reference field is a list, because there are multiple UIDs in it, and so I assume that I need an 'add item to list' action. But when I select the variable for my node which was fetched by the component, I don't see the user reference field of that node. That's where I'm stuck. How can I fix this?

Related question: In the Settings dialog in a component, you can set names of Parameter or Provided variables. It LOOKS like the Fetched Entity variables returned from, for example, the 'Fetch Entity by ID' action are automatically assigned to the variables established in the Settings dialog. Is this correct?

And if not, how do I assign variables returned by actions to the variables in the Settings dialog?

Thanks once again!

To simplify my complicated

jbucks's picture

To simplify my complicated query above:

  1. What is the 'correct' way to use Rules to update the value of a User Reference (or Node Ref)? Is it through the 'Add item to list' action?

  2. If I have found a node using 'fetch entity by ID', and that node has a User Reference field, how can I access that field in order to update it with Rules?

Thanks again for any tips!

Rules

Group organizers

Group categories

Categories

Group notifications

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